使用不同类中的多个数组列表

时间:2019-02-25 20:08:10

标签: java

对,我正在尝试读取文件,并将该数据存储在arraylist( primaryList.class 中的 primaryList )中。数据只是国家,城市及其人口的数据。以下是一个示例: USA; Chicago = 2695000;

存储数据后,我在第二个类( secondaryList.java )中使用了分别称为 setPopulation getPopulation 的方法来分隔总体数字并将其存储在自己的数组列表中,即 secondaryList.java 中的 secondaryList

如果arraylists在同一个类中,则一切正常。但是,当我分开列表时, primaryList 会被清空。我一直在阅读,如果正确的话,如何调用构造函数似乎存在问题,但是我不确定如何在代码中实现它。

Main.java -运行其他两个类中的方法的类

public class Main {
public static void main(String[] args) throws Exception {
    primaryList primaryList = new primaryList();
    secondaryList secondaryList = new secondaryList();

    primaryList.ReadFile();

    secondaryList.setPopulation(0);

    String country = secondaryList.getPopulation(0);

    System.out.println(country);
}}

secondaryList.java -负责提取字符串的填充部分并将其存储在 secondaryList

中的类
public class secondaryList {
primaryList primaryList = new primaryList();

private String country;
private ArrayList<String> secondaryList;

public secondaryList() {
    secondaryList = new ArrayList<String>();
}

public void setPopulation(int i) {
    System.out.println("starting setPopulation");

    int listSize = primaryList.GetInputListSize();
    System.out.println("primaryList size is: " + listSize);

    country = primaryList.GetInputList(i);

    System.out.println("setPopulation got:" + country);
    String resultPopulation = country.substring(country.indexOf("=") + 1);

    secondaryList.add(resultPopulation);
}

public String getPopulation(int i) {

    country = secondaryList.get(i);

    return country;
}}

primaryList.java -读取文件并负责将其内容存储在 primaryList 列表中的类。

public class primaryList {

private List<String> primaryList; // List for entire row
private String country;

public primaryList() {
    primaryList = new ArrayList<String>();
}

public void ReadFile() throws IOException {
    FileReader reader = new FileReader("FileToRead.txt");

    BufferedReader bufferedReader = new BufferedReader(reader);

    // Read file FileToRead.txt and add to countries list
    while ((country = bufferedReader.readLine()) != null) {
        SetInputList(country);
    }
    bufferedReader.close();
    System.out.println(primaryList.size());
}

public String GetInputList(int i) {
    country = primaryList.get(i);

    return country;
}

public void SetInputList(String country) {
    primaryList.add(country);
}

public int GetInputListSize() {
    int i = 0;

    i = primaryList.size();

    return i;
}}

输出:运行程序时,得到以下输出:

primaryList size is: 11
starting setPopulation
primaryList size is: 0
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
    at java.util.ArrayList.rangeCheck(Unknown Source)
    at java.util.ArrayList.get(Unknown Source)
    at testing.primaryList.GetInputList(primaryList.java:32)
    at testing.secondaryList.setPopulation(secondaryList.java:22)
    at testing.Main.main(Main.java:10)

请注意,在尝试从 secondaryList 调用 primaryList.java 来获取 primaryList 的大小之后,列表为空的情况。

我在做什么错?任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您正在创建primaryList的新实例,并尝试在secondaryList中读取它。

public class secondaryList {
primaryList primaryList = new primaryList(); //New instance of primaryList

private String country;
private ArrayList<String> secondaryList;

public secondaryList() {
    secondaryList = new ArrayList<String>();
}

public void setPopulation(int i) {
    System.out.println("starting setPopulation");

    int listSize = primaryList.GetInputListSize(); //Reading from new instance, size is 0
    System.out.println("primaryList size is: " + listSize);

    country = primaryList.GetInputList(i);

    System.out.println("setPopulation got:" + country);
    String resultPopulation = country.substring(country.indexOf("=") + 1);

    secondaryList.add(resultPopulation);
}

您必须引用primaryList方法中存储输入的main。这将需要您稍微重组代码。尝试在primaryList中包含类型为setPopulation的新参数,然后将对初始primaryList的引用传递给它。