0

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?

2
  • 1
    You delete everything inside of insertionList after adding it to zeroMatrix. What did you expect it to print out after doing that? Commented Sep 5, 2014 at 3:31
  • with in while loop, you should not clear insertionList instead you should create a new instance of insertionList Commented Sep 5, 2014 at 3:31

2 Answers 2

2

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>();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Elliott! I incorrectly thought insertionList would create distinct copies of itself. You are correct, I should be creating new instances insertionList.
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.