2014-09-05 53 views
0

你好,我有一個問題,將一個整數的ArrayList存儲到整數ArrayList的ArrayList中。下面是完整的代碼:當我添加一個包含整數的對象時,Java 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()); 
    } 

} 

這個節目是從包含一個文本文件「matrixInput.txt」閱讀:

34 
20 

的問題是我加insertionList後進入zeroMatrix,zeroMatrix打印空ArrayList(在代碼的最後一行)。我懷疑它是因爲我沒有正確地將insertionList插入到zeroMatrix中?或者,也許我打印不正確?

+1

刪除'insertionList'中的所有內容後,將其添加到'zeroMatrix'。這樣做後,你期望它打印出什麼? – csmckelvey 2014-09-05 03:31:06

+0

在while循環中,您不應該清除insertionList,而應該創建一個新的插入實例 – 2014-09-05 03:31:42

回答

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>(); 
+0

謝謝Elliott!我錯誤地認爲插入列表會創建自己的不同副本。你是對的,我應該創建新的插件實例。 – greenleaftreee 2014-09-05 04:08:27

0

代碼中的問題在於你如何確定變量的範圍。現在,insertionList是一個變量,它應該在你的outer while循環的範圍內,所以不要事先聲明它,而是稍後清除它或稍後重新分配它,只需在inner 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()); 

}