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?
insertionListafter adding it tozeroMatrix. What did you expect it to print out after doing that?