0

I want to create an ArrayList-matrix with n-rows and m-columns, for example

   1 2 3 4 5

   1 2 3 4 5 

   1 2 3 4 5

   1 2 3 4 5

   1 2 3 4 5

I've already written a code for creating such a matrix but my code doesn't display the values when I clear the list containing the column-data. Here is my

package testproject;

import java.util.ArrayList;

public class TestProject {

    public static void main(String[] args) {
        ArrayList<Integer> intList = new ArrayList<>();
        ArrayList<ArrayList<Integer>> mainList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            for (int k = 0; k < 5; k++) {
                intList.add(k);
            }
            mainList.add(intList);
            intList.clear(); //this line is probably the problem
        }
        for (int row = 0; row < mainList.size(); row++) {
            for (int col = 0; col < mainList.get(0).size(); col++) {
                System.out.print(mainList.get(row).get(col) + ",");
            }
            System.out.println("");
        }
    }
}

Is there any possibility to clear the contents of intList without clearing the contents of mainList??

1
  • Couldn't you just declare a new ArrayList<Integer> newList = new ArrayList<Integer>(intList); Then add that to the mainList instead of intList? Commented May 6, 2016 at 18:02

6 Answers 6

1

When calling mainList.add(intList); you are adding a reference pointing to the intList object into your mainList rather than copying the values. You will have to create one instance of "intList" per row, and not clear anything.

Sign up to request clarification or add additional context in comments.

2 Comments

sandBo00 I see... creating one instance for each row would probably kill the performance at higher amounts of rows (>=100000).
@Ramses I just tested with 120000 rows and 10 columns each, every position filled with a random integer, and that took next to no time on my machine ;-)
1

Yes, your hunch is correct. the line intList.clear(); is the problem. Because the intList is stored in mainList and if you clear intList the information in mainList also gets lost. one Solution: create a new intList in every loop But because matrizes are usually not variable in their dimensions - which Arraylists are - you should consider using int[][] intList for your matrizes.

Comments

1

Java works with references. So in your program, mainList will contain 5 references to the same unique intList. Anything you do to intList will reflect in all the "rows" in mainList, clearing, changing a value, etc.

If you want to create a matrix you most likely want to have each "row" be a reference to a different list. This can be done by instantiating a new list in the loop:

package testproject;

import java.util.ArrayList;

public class TestProject {

    public static void main(String[] args) {
        ArrayList<ArrayList<Integer>> mainList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            ArrayList<Integer> intList = new ArrayList<>(); // This creates a new list for each row.
            for (int k = 0; k < 5; k++) {
                intList.add(k);
            }
            mainList.add(intList);
        }
        for (int row = 0; row < mainList.size(); row++) {
            for (int col = 0; col < mainList.get(0).size(); col++) {
                System.out.print(mainList.get(row).get(col) + ",");
            }
            System.out.println("");
        }
    }
}

In your program, clearing the intermediate list isn't necessary.

Comments

1

Just need to move the creation of intList inside for loop and now should work.

import java.util.ArrayList;
        public class TestProject {
            public static void main(String[] args) {
                ArrayList<ArrayList<Integer>> mainList = new ArrayList<>();
                for(int i=0;i<10;i++) {
                    ArrayList<Integer> intList = new ArrayList<>();
                    for(int k=0;k<5;k++) {
                        intList.add(k);
                    }
                    mainList.add(intList);
                }
                for(int row=0;row<mainList.size();row++) {
                    for(int col=0;col<mainList.get(0).size();col++) {
                        System.out.print(mainList.get(row).get(col)+",");
                    }
                    System.out.println("");
                }
            }
        }

2 Comments

You have to remove the intList.clear(); line, or else the lists will still be empty.
@Maxqueue your solution only works when you delete the row intList.clear() :-)
0

I would create n- or m- ArrayLists it depends how You want to use it.

Or You create one hashmap > for each row one ArrayList.

Comments

0

This is my purpose to show 4*4 Matrix on arrayList

import java.util.ArrayList;
import java.util.Collections;

/**
 *
 * @author David Clavijo
 */
public class PuzzleShuffle {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        ArrayList<Integer> blockNums = new ArrayList<>();
        
        
                blockNums.add(1);
                blockNums.add(2);
                blockNums.add(3);
                blockNums.add(4);
                blockNums.add(5);
                blockNums.add(6);
                blockNums.add(7);
                blockNums.add(8);
                blockNums.add(9);
                blockNums.add(10);
                blockNums.add(11);
                blockNums.add(12);
                blockNums.add(13);
                blockNums.add(14);
                blockNums.add(15);
                blockNums.add(0);
                
                Collections.shuffle(blockNums);
                for(Integer i: blockNums){
                if (blockNums.indexOf(i)%4 == 0 && blockNums.indexOf(i)!=0){  
                        System.out.println("\n");}
                System.out.printf("%4d", i);
                
                }
                System.out.println("\n"+"\n"+"Matriz Random");
                
    }
    
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.