Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/399.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从多个类访问和修改类中的ArrayList/TreeSet_Java_Android_Arrays_Arraylist - Fatal编程技术网

Java 从多个类访问和修改类中的ArrayList/TreeSet

Java 从多个类访问和修改类中的ArrayList/TreeSet,java,android,arrays,arraylist,Java,Android,Arrays,Arraylist,我是java新手,我正在尝试访问与其他类不同的类中的ArrayList和TreeSet。 我知道从一个类访问它的方法 这是我有树集的班级: public class Gestor { private TreeSet<User> gestorTSusers; public Gestor (){ this.gestorTSusers = new TreeSet<>(); }

我是java新手,我正在尝试访问与其他类不同的类中的ArrayList和TreeSet。 我知道从一个类访问它的方法

这是我有树集的班级:

 public class Gestor {
        private TreeSet<User> gestorTSusers;

        public Gestor (){
            this.gestorTSusers = new TreeSet<>();
        }          

        public TreeSet<User> getGestorTSusers() {
            return gestorTSusers;
        }

        public void setGestorTSusers(TreeSet<User> gestorTSusers) {
            this.gestorTSusers = gestorTSusers;
        }
    }
我是否使用相同的
树集
上下文是否会有用? 做这样的事情最好、最有效的方法是什么


很抱歉,我知道这看起来很糟糕,但这是我第一次问,所以请给我一个机会。

如果我正确理解您的问题,您只需要一个Gestor实例,并在多个课程中使用它。当您将Gestor设置为单例时,您将有相同的TreeSet实例绑定到Gestor。尝试理解这里的单例设计模式的含义。

您需要在gestor对象中设置树集,如下所示

Gestor gestor = new Gestor();
gestor.setGestorTSusers(treeSetYouWantToUse)
您还可以在Gestor类中创建一个构造函数,该构造函数接受树集

public class Gestor {
        private TreeSet<User> gestorTSusers;

        public Gestor (TreeSet treeSetYouWantToUse){
            this.gestorTSusers = treeSetYouWantToUse;
        }          

        public TreeSet<User> getGestorTSusers() {
            return gestorTSusers;
        }

        public void setGestorTSusers(TreeSet<User> gestorTSusers) {
            this.gestorTSusers = gestorTSusers;
        }
    }

公共类管理器{
私人树丛管理者;
公共管理者(TreeSet treeSetYouWantToUse){
this.gestorsusers=treeSetYouWantToUse;
}          
公共树集getgestorsusers(){
返回手势使用者;
}
公共无效集合集合集合(树集合集合集合集合){
this.gestortsuers=gestortsuers;
}
}

问题是,我要使用的树集是我在Gestor类(
gestorTSusers
)中创建的树集,因此将树集设置为自身是没有意义的。构造函数的情况也差不多。你知道我想去哪里吗?如果我遗漏了什么,请告诉我,并感谢我现在看到的反馈。我喜欢@Juseeth中的单例,但如果您不想在MainActivity类中使用getter返回
this.gestor
this.gestor.getgestortsuers()
您是指返回我在MainActivity中创建的实例的arrayList
gestortsuers
的公共方法吗?然后我可以将更新后的数组发送回MainActivity,并通过另一个方法传递它,该方法将更新Gestor类中的实际ArrayList?我看到了链接,它似乎是一个解决方案,但我相信它不是最好的。我会做更多的研究,不过还是要谢谢你。
Gestor gestor = new Gestor();
gestor.setGestorTSusers(treeSetYouWantToUse)
public class Gestor {
        private TreeSet<User> gestorTSusers;

        public Gestor (TreeSet treeSetYouWantToUse){
            this.gestorTSusers = treeSetYouWantToUse;
        }          

        public TreeSet<User> getGestorTSusers() {
            return gestorTSusers;
        }

        public void setGestorTSusers(TreeSet<User> gestorTSusers) {
            this.gestorTSusers = gestorTSusers;
        }
    }