简体   繁体   English

Java如何从另一个类访问另一个ArrayList?

[英]Java how to access another ArrayList from another class?

I tried the getter method ( create a getter method is this class and return the object so you can use it in another class) but when I run it's just an empty ArrayList 我尝试了getter方法(创建此类的getter方法并返回该对象,以便可以在另一个类中使用它),但是当我运行它时,它只是一个空的ArrayList

In this example I have the user to input the code (1, 2 ,3 , ...) and a random name then store that value in an ArrayList list but when I create an instance of that class and use the getList() method it return an empty ArrayList. 在此示例中,我让用户输入代码(1、2、3,...)和一个随机名称,然后将该值存储在ArrayList列表中,但是当我创建该类的实例并使用getList()方法时它返回一个空的ArrayList。 This is the code: 这是代码:

SubTestSite1 SubTestSite1

package testsite;

public class SubTestSite1 {

private int id;
private String name;

@Override
public String toString() {
    return this.getId() + "   " + this.getName()+"    ";
}

public SubTestSite1() {
}

public SubTestSite1(int id, String name) {
    this.id = id;
    this.name = name;
}

public int getId() {
    return id;
}

public String getName() {
    return name;
}

public void setId(int id) {
    this.id = id;
}

public void setName(String name) {
    this.name = name;
}
}

SubTestSite2 SubTestSite2

package testsite;

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Hashtable;

public class SubTestSite2 {
Hashtable hash = new Hashtable(100);
ArrayList list = new ArrayList(100);
Scanner sc = new Scanner(System.in);    

public ArrayList getList() {
    return list;
}

public void add() {
    int id;
    String name, choice = null, customer;
    do {
        System.out.print("enter id: ");
        id = Integer.parseInt(sc.nextLine());
        System.out.print("enter name: ");
        name = sc.nextLine().trim().toUpperCase();
        SubTestSite1 sts1 = new SubTestSite1(id, name);
        list.add(sts1);
        System.out.println("Continue? Y/N");
        choice = sc.nextLine();
    } while (choice.equalsIgnoreCase("y"));
    System.out.print("Customer name: ");
    customer = sc.nextLine();
    hash.put(customer, list);
    SubTestSite3 sts3 = new SubTestSite3();
    sts3.displayOrder(this.hash, customer);
}
}

SubTestSite3 SubTestSite3

package testsite;

import java.util.Hashtable;
import java.util.ArrayList;
import java.util.Scanner;


public class SubTestSite3 {

public void displayOrder(Hashtable hash, String name) {
    SubTestSite2 sts2 = new SubTestSite2();
    System.out.println(sts2.getList());

}
}

The main 主要的

package testsite;

public class TestSite {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    SubTestSite2 sts2 = new SubTestSite2();
    sts2.add();
}
}

Don't mind the Hashtable though, I just wanna play around with it :P Thanks! 不过不要介意Hashtable,我只想玩它:P谢谢!

I don't see your main method, so I'm guessing that you are creating a SubTestSite2 object from main and calling add() on it there. 我看不到您的main方法,所以我猜您正在从main创建一个SubTestSite2对象,并在那里调用add() Let's call that object mainsts for clarity. 为了清楚起见,我们将该对象mainsts If this is the case, you add the items to mainsts's list, then create a new SubTestSite2 object, sts2 , and attempt to read sts2's list, not mainsts's . 如果是这种情况,可以将项目添加到mainsts的列表中,然后创建一个新的SubTestSite2对象sts2 ,并尝试读取sts2's列表,而不是mainsts's列表。 Since you did not declare the ArrayList as static, each object, mainsts and sts2 in this case, has its own list, so the list for sts2 would indeed be empty, but mainsts's would not be. 由于您没有将ArrayList声明为静态,因此在这种情况下,每个对象,mainsts和sts2都有其自己的列表,因此sts2的列表确实为空,而mainsts的列表则为空。

When you declared the ArrayList as static , you made it into a class variable rather than an instance variable. 当您将ArrayList声明为static ,您将其变成了类变量而不是实例变量。 As 'static', all objects that use that class will use that variable. 作为“静态”,使用该类的所有对象都将使用该变量。 In other words, mainsts and sts2 here would share the same list. 换句话说,这里的mainsts和sts2将共享相同的列表。 If you want this, I would say access the list directly through the class with SubTestSite2.list 如果您愿意,我会说直接通过SubTestSite2.list类访问列表。

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

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