如何在Java中创建ArrayList列表?

问题描述 投票:1回答:2

我正在尝试列出一个列表,但我注意到我的列表在循环结束时结束为空。

ArrayList<ArrayList<Integer>> list_of_lists = new ArrayList<ArrayList<Integer>>();
    List<Integer> list = new ArrayList<Integer>();          
    for(int x = 0; x < 5; x++) {
        for(int y = 0; y < 2; y++) {                
            list.add(x);
            list.add(y);                
            list_of_lists.add((ArrayList<Integer>) list);
            System.out.println(list_of_lists);
            list.clear();
        }
    }
    System.out.println(list_of_lists);

但是,在控制台中,结果是:

[[0, 0]]
[[0, 1], [0, 1]]
[[1, 0], [1, 0], [1, 0]]
[[1, 1], [1, 1], [1, 1], [1, 1]]
[[2, 0], [2, 0], [2, 0], [2, 0], [2, 0]]
[[2, 1], [2, 1], [2, 1], [2, 1], [2, 1], [2, 1]]
[[3, 0], [3, 0], [3, 0], [3, 0], [3, 0], [3, 0], [3, 0]]
[[3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
[[4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0], [4, 0]]
[[4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1], [4, 1]]
[[], [], [], [], [], [], [], [], [], []]

为什么,决赛是一个空列表而不是:

[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]

这是彼此而不是两个单独的列表。我的代码将一个列表链接到另一个列表

list arraylist
2个回答
1
投票

最后一个列表空的原因是Java以引用的形式存储所有内容,即以内存地址的形式,并以相同的方式操作它们。

因此,ArrayList对象列表也是一个引用。所以

1)每次运行内部循环时,都会将对list对象的引用添加到ArrayList list_of_lists中。

2)因此,每次操作列表时,list_of_lists将列表的值存储为内循环运行的次数,即列表被添加到其中。

因此,每次调用System.out.println(list_of_lists)

1)它打印list同样没有添加到list_of_lists的次数。

2)在此之后使用list.clear()清除列表,这使得对list的所有引用都为空。

您还可以检查此代码以在每次迭代时获取list_of_lists,以清楚地了解正在发生的事情。

ArrayList<ArrayList<Integer>> list_of_lists = new ArrayList<ArrayList<Integer>>();
List<Integer> list = new ArrayList<Integer>();          
for(int x = 0; x < 5; x++) {
    for(int y = 0; y < 2; y++) {                
        list.add(x);
        list.add(y);                
        list_of_lists.add((ArrayList<Integer>) list);
        System.out.println(list_of_lists);
        list.clear();
    }
    System.out.println(list_of_lists); //composes of empty lists 
                                        //the same of times list is added to it
}
System.out.println(list_of_lists);

**解:*

要创建列表列表,每次将新列表添加到list_of_lists时都必须分配内存:

  ArrayList<ArrayList<Integer>> list_of_lists = new 
    ArrayList<ArrayList<Integer>>();
        List<Integer> list;        
        for(int x = 0; x < 5; x++) {
            for(int y = 0; y < 2; y++) {  
                list = new ArrayList<>();              
                list.add(x);
                list.add(y);                
                list_of_lists.add((ArrayList<Integer>) list);
                System.out.println(list_of_lists);
                // list.clear();
            }
         }
          System.out.println(list_of_lists);
        }

1
投票

这是因为所有子列表都指向同一个实例。您应该为每对数字创建新实例,而不是重用list。检查下面的代码

    ArrayList<ArrayList<Integer>> list_of_lists = new ArrayList<ArrayList<Integer>>();
    List<Integer> list;
    for(int x = 0; x < 5; x++) {
        for(int y = 0; y < 2; y++) {
            list = new ArrayList<Integer>();
            list.add(x);
            list.add(y);
            list_of_lists.add((ArrayList<Integer>) list);
            System.out.println(list_of_lists);
            //list.clear();
        }
    }
    System.out.println(list_of_lists);

输出将是

[[0, 0]]
[[0, 0], [0, 1]]
[[0, 0], [0, 1], [1, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]
[[0, 0], [0, 1], [1, 0], [1, 1], [2, 0], [2, 1], [3, 0], [3, 1], [4, 0], [4, 1]]
© www.soinside.com 2019 - 2024. All rights reserved.