简体   繁体   English

创建ArrayList <Integer>的ArrayList

[英]Create an ArrayList of ArrayList<Integer>

I'm trying to use the code below to create a multidimensional ArrayList. 我正在尝试使用下面的代码来创建一个多维ArrayList。 My code populates the inner ArrayList (localSolutions) just fine, but when I try an add that ArrayList to the outer ArrayList (solutions), something goes wrong, and it adds empty ArrayLists instead. 我的代码填充内部ArrayList(localSolutions)就好了,但是当我尝试将ArrayList添加到外部ArrayList(解决方案)时,出现了问题,并且它添加了空ArrayLists。

public class MathCapstone {

public static void main(String[] args) {
    ArrayList<ArrayList<Integer>> list = entireList(10);

    for(int q = 0;q<list.size();q++) {
        System.out.println(list.get(q));
    }

public static ArrayList<ArrayList<Integer>> entireList(int max) {
    ArrayList<ArrayList<Integer>> solutions = new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> localSolutions = new ArrayList<Integer>();

    for(int i = 1; i <= max; i++) {
        for(int j = 1; j < i; j++) {
           //System.out.println(j + "mod" + i + "=" + (j*j)%i);
            if ((j*j)%i == 1) {
                localSolutions.add(j);
            }
        }
        //System.out.println(localSolutions.toString());
        solutions.add(localSolutions);
        localSolutions.clear();
    }
    return solutions;
}

On a final note: would it be better to use a HashMap of ArrayLists (eventually I'm going to be creating CDF's for max values up to about 10k)? 最后要说明的是:使用ArrayLists的HashMap会不会更好(最终我将创建最大值约为10k的CDF)?

You are clearing the localSolutions list. 您正在清除localSolutions列表。

In Java you copy by value only the reference to an Object not the actual object itself. 在Java中,您只能通过值复制对Object的引用而不是实际对象本身。 So when you add the localSolutions list in your solutions list, both the localSolutions reference and the first entry of the solutions list, point to the same object. 因此,当您在解决方案列表中添加localSolutions列表时, localSolutions引用和解决方案列表的第一个条目都指向同一个对象。

Thus, when you clear the localSolutions list, you effectively clear the first entry on your solutions list. 因此,清除localSolutions列表时,可以有效清除解决方案列表中的第一个条目。

You're doing: 你在做:

localSolutions.clear();

Adding a list to another list doesn't add a copy of the list, it adds the same list object. 将列表添加到另一个列表不会添加列表的副本,它会添加相同的列表对象。 What your code is doing in the outerloop is filling the same list with elements, emptying it, and adding it to solutions . 您的代码在外环中执行的操作是使用元素填充相同的列表,清空它并将其添加到solutions solutions contains max references to the same, empty list. solutions包含对同一个空列表的max引用。

What you want to do is: 你想要做的是:

ArrayList<ArrayList<Integer>> solutions = new ArrayList<ArrayList<Integer>>();
for(int i = 1; i <= max; i++) {
    ArrayList<Integer> localSolutions = new ArrayList<Integer>();
    for(int j = 1; j < i; j++) {
       //System.out.println(j + "mod" + i + "=" + (j*j)%i);
        if ((j*j)%i == 1) {
            localSolutions.add(j);
        }
    }
    //System.out.println(localSolutions.toString());
    solutions.add(localSolutions);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM