Creating a Matrix with ArrayLists

可紊 提交于 2020-01-06 15:06:28

问题


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:


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.




回答2:


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.




回答3:


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.




回答4:


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



回答5:


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.



来源:https://stackoverflow.com/questions/37078635/creating-a-matrix-with-arraylists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!