简体   繁体   中英

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

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. This is the code:

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

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

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!

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. Let's call that object mainsts for clarity. 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 . 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.

When you declared the ArrayList as static , you made it into a class variable rather than an instance variable. As 'static', all objects that use that class will use that variable. In other words, mainsts and sts2 here would share the same list. If you want this, I would say access the list directly through the class with SubTestSite2.list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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