Reputation: 115
Hello I'm having an issue storing an ArrayList of Integers into an ArrayList of ArrayList of Integers. Here is the full code:
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());
}
}
This program is reading from a text file "matrixInput.txt" that contains:
34
20
The problem is after I added insertionList into zeroMatrix, zeroMatrix prints an empty ArrayList (during the last line of the code). I suspect its because I'm not inserting insertionList correctly into zeroMatrix? Or maybe I'm printing it incorrectly?
Upvotes: 0
Views: 1040
Reputation: 762
The issue in your code is in how you scope your variables. Now, insertionList is a variable which should have been inside the scope of your outer while loop, so instead of declaring it beforehand and clearing it afterwards or reassigning it later, just declare and instantiate it inside the inner while loop.
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());
}
Upvotes: 0
Reputation: 201467
You aren't adding a copy of the List
, just a reference. So when you do,
zeroMatrix.add(insertionList);
insertionList.clear(); // <-- this
You clear the List you added to the zeroMatrix
. You can copy the List
,
zeroMatrix.add(new ArrayList<>(insertionList)); // <-diamond operator, see below.
insertionList.clear(); // <-- now you can clear the insertionList.
Or you can move the insertionList
declaration into your loop body -
while(matrixScanner.hasNextLine()){
ArrayList<Integer> insertionList = new ArrayList<Integer>();
In Java 7 or above you can use the diamond operator -
ArrayList<Integer> insertionList = new ArrayList<>();
For Java 6 and earlier you had to specify the type on both sides like
ArrayList<Integer> insertionList = new ArrayList<Integer>();
Upvotes: 2