简体   繁体   English

从另一个类访问arraylist

[英]Access arraylist from another class

I am a newbie to Java and I have a gui class which has a GUI component and it takes the input from the text field and should pass it to another class. 我是Java的新手,我有一个gui类,该类具有GUI组件,它从文本字段获取输入,并将其传递给另一个类。 The action listener of the button is below. 该按钮的动作监听器在下面。

public void actionPerformed(ActionEvent action) {               
  arraylist.add(textField_1.getText());
  arraylist.add(textField_2.getText());
  arraylist.add(textField_3.getText());
  arraylist.add(textField_4.getText());     
}

since it is a void method I cannot return the array list so that Ii cannot construct a getter. 由于这是一个void方法,因此我无法返回数组列表,因此Ii无法构造一个吸气剂。

public ArrayList<String> getList(){
    return this.arraylist;
}

Could anyone please tell me how to access this arraylist from the another class without passing it through the constructor? 谁能告诉我如何从另一个类访问此数组列表而不将其传递给构造函数? I am sorry if i asked anything wrong. 如果我问错了,我很抱歉。 Thanks in advance. 提前致谢。

This is one of the many possible approaches. 这是许多可能的方法之一。

Just define another class and call the setter from your actionPerformed(..) method. 只需定义另一个类,然后从您的actionPerformed(..)方法中调用setter

public class YourOtherClass {
    private static ArrayList<String> arraylist;

    public void setList(arrayList) {
        this.arraylist = arraylist;
    }

    public ArrayList<String> getList() {
        return this.arraylist;
    }
}

Now you can simply set this arraylist as: 现在,您可以简单地将此arraylist设置为:

public void actionPerformed(ActionEvent action) {               
    arraylist.add(textField_1.getText());
    arraylist.add(textField_2.getText());
    arraylist.add(textField_3.getText());
    arraylist.add(textField_4.getText());     
    YourOtherClass.setList(arraylist);
}

Now when you want to access the contents of this list, simply use: 现在,当您要访问此列表的内容时,只需使用:

...
//any other method
ArrayList<String> arraylist = YourOtherClass.getList();
System.out.println(arraylist.get(0)); //or whatever
...

If I understood, you want to do it: 如果我理解,您想这样做:

public class A {
    private ArrayList<String> arrayList;

    public ArrayList<String> getArrayList() {
        return this.arrayList;
    }
}


public class B {
    private A a = new A();

    public void actionPerformed(ActionEvent action) {               
        a.getArrayList().add(textField_1.getText());
        a.getArrayList().add(textField_2.getText());
        a.getArrayList().add(textField_3.getText());
        a.getArrayList().add(textField_4.getText());     
    }
}

You can make that arraylist as Static and access it. 您可以将该数组列表设置为“静态”并访问它。

To access that particular arraylist use the below syntax 要访问该特定数组列表,请使用以下语法

classNameThatContainArraylist.yourArrayList

be careful while using static. 使用静态时要小心。

If you want to use to set and get data then there are many approaches and two of them are follow 如果您想使用设置和获取数据,那么有很多方法,下面是其中两种

 public class SetDataInArrayList {

//Aproach one by using object
        private List<ActionEvent> list;
public SetDataInArrayList() {
        list = new ArrayList();
    }

    public void setDataInList(ActionEvent e) {
        list.add(e);
    }

    public List<ActionEvent> getList() {
        return list;
    }

//Approach two by using static reference
private static List<ActionEvent> newList;

    static {
        newList = new ArrayList<>();
    }

    public static void add(ActionEvent e) {
        newList.add(e);
    }

    public static List<ActionEvent> returnList() {
        return newList;
    }

if you use either of approach you will need reference variable in both of cases to fetch data 如果您使用两种方法之一,则在两种情况下都需要引用变量来获取数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM