添加包含整数的对象时,Java ArrayList保持为空

时间:2014-09-05 03:29:18

标签: java arraylist java.util.scanner

您好我在将Integers的ArrayList存储到Integers的ArrayList的ArrayList中时遇到了问题。这是完整的代码:

public class SetZeroMatrix {

  public static void main(String [] args){
    ArrayList<ArrayList<Integer>> zeroMatrix = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> insertionList = new ArrayList<Integer>();

    try {
        FileReader in = new FileReader("matrixInput.txt");
        BufferedReader br = new BufferedReader(in);
        Scanner matrixScanner = new Scanner(br);

         while(matrixScanner.hasNextLine()){
            Scanner rowReader = new Scanner(matrixScanner.nextLine());
            while(rowReader.hasNextInt()){
                insertionList.add(rowReader.nextInt());
            }
            //testing to see if insertionList is empty
            System.out.println("Arraylist contains: " + insertionList.toString()); 
            zeroMatrix.add(insertionList);
            insertionList.clear();
         }
         matrixScanner.close();
      }
      catch(FileNotFoundException ex) {
            System.out.print("File not found" + ex);
      }
      //testing to see if zeroMatrix is empty
      ArrayList<Integer> testList = new ArrayList<Integer>();
      testList = zeroMatrix.get(1);
      System.out.println("ArrayList contains: " + testList.toString()); 
  }

}

该程序正在读取文本文件&#34; matrixInput.txt&#34;包含:

34
20

问题是在我将insertionList添加到zeroMatrix之后,zeroMatrix打印出一个空的ArrayList(在代码的最后一行)。我怀疑它是因为我没有正确地将insertList插入zeroMatrix?或者我可能不正确地打印它?

2 个答案:

答案 0 :(得分:2)

您没有添加List的副本,只是一个参考。所以,当你这样做时,

zeroMatrix.add(insertionList);
insertionList.clear(); // <-- this

清除添加到zeroMatrix的列表。您可以复制List

zeroMatrix.add(new ArrayList<>(insertionList)); // <-diamond operator, see below.
insertionList.clear(); // <-- now you can clear the insertionList.

或者您可以将insertionList声明移动到循环体中 -

while(matrixScanner.hasNextLine()){
  ArrayList<Integer> insertionList = new ArrayList<Integer>();

在Java 7或更高版本中,您可以使用菱形运算符 -

  ArrayList<Integer> insertionList = new ArrayList<>();

对于Java 6及更早版本,您必须在两侧指定类型,如

  ArrayList<Integer> insertionList = new ArrayList<Integer>();

答案 1 :(得分:0)

代码中的问题在于如何确定变量的范围。现在,insertList是一个变量,应该在你的外部while循环的范围内,所以不是事先声明它并之后清除它或稍后重新分配它,只需在内部while循环中声明并实例化它。

public class SetZeroMatrix{

  public static void main(String [] args){
    ArrayList<ArrayList<Integer>> zeroMatrix = new ArrayList<ArrayList<Integer>>();

    try{
        FileReader in = new FileReader("matrixInput.txt");
        BufferedReader br = new BufferedReader(in);
        Scanner matrixScanner = new Scanner(br);

        while(matrixScanner.hasNextLine()){
            Scanner rowReader = new Scanner(matrixScanner.nextLine());
            ArrayList<Integer> insertionList = new ArrayList<Integer>();    
            while(rowReader.hasNextInt()){
                insertionList.add(rowReader.nextInt());
            }
            //testing to see if insertionList is empty
            System.out.println("Arraylist contains: " + insertionList.toString()); 
            zeroMatrix.add(insertionList);
            insertionList.clear();
        }

        matrixScanner.close();
    }
    catch(FileNotFoundException ex){
        System.out.print("File not found" + ex);
    }
    //testing to see if zeroMatrix is empty
    ArrayList<Integer> testList = new ArrayList<Integer>();
    testList = zeroMatrix.get(1);
    System.out.println("ArrayList contains: " + testList.toString()); 

}