ArrayList 未显示正确大小的问题

发布于 2024-12-02 18:55:27 字数 830 浏览 3 评论 0 原文

所以我得到了这段代码,如果 ArrayList 不存在,它将把数字 1-9 添加到单独的 ArrayList 中。然而,即使我打印了 ArrayLists(并且它得到了所有正确的数字),当我打印 ArrayList 的 .size 时,它​​给了我 1 而不是 9。我希望你理解我的问题。这是代码:

ArrayList[][] tillatnaSiffror = new ArrayList[9][9];

    for(int i=0;i<9;i++){
        for(int ruta=0;ruta<9;ruta++){

            if(tillatnaSiffror[i][ruta] == null){
                for(int add=1;add<=9;add++){
                    tillatnaSiffror[i][ruta] = new ArrayList<Integer>();
                    tillatnaSiffror[i][ruta].add(add);
                    System.out.println(tillatnaSiffror[i][ruta]);
                }
                System.out.println(tillatnaSiffror[i][ruta].size());
            }
        }
    }

这给了我这个(当然虽然九次):[1][2][3][4][5][6][7][8][9]1

现在我想知道,为什么当我打印 .size 时得到 1 而不是 9?

So I've got this code which will add the numbers 1-9 into separate ArrayLists if the ArrayList doesn't exist. However, even though I print the ArrayLists(and it gets me all the correct numbers), when I print the .size of the ArrayList, it gives me 1 instead of 9. I hope you understand my problem. Here's the code:

ArrayList[][] tillatnaSiffror = new ArrayList[9][9];

    for(int i=0;i<9;i++){
        for(int ruta=0;ruta<9;ruta++){

            if(tillatnaSiffror[i][ruta] == null){
                for(int add=1;add<=9;add++){
                    tillatnaSiffror[i][ruta] = new ArrayList<Integer>();
                    tillatnaSiffror[i][ruta].add(add);
                    System.out.println(tillatnaSiffror[i][ruta]);
                }
                System.out.println(tillatnaSiffror[i][ruta].size());
            }
        }
    }

That gives me this(although nine times of course): [1][2][3][4][5][6][7][8][9]1

Now I'm wondering, WHY do I get 1 instead of 9 when I print .size?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人疚 2024-12-09 18:55:27

因为您在每次迭代中重置列表

tillatnaSiffror[i][ruta] = new ArrayList<Integer>();

,即创建一个新列表,为添加的每个数字丢弃前一个列表!

尝试移出列表的创建:

 ,-->   tillatnaSiffror[i][ruta] = new ArrayList<Integer>();
 |      for(int add=1;add<=9;add++){
 '-----<
            tillatnaSiffror[i][ruta].add(add);
            System.out.println(tillatnaSiffror[i][ruta]);
         }

Ideone.com demo


作为旁注,我建议在这里避免使用数组,而完全使用 Java 集合。例如,考虑使用像 List>> 这样的结构。

Because you reset the list in each iteration by doing

tillatnaSiffror[i][ruta] = new ArrayList<Integer>();

i.e., you create a new list, throwing away the previous one for each digit you add!

Try moving out the creation of the list:

 ,-->   tillatnaSiffror[i][ruta] = new ArrayList<Integer>();
 |      for(int add=1;add<=9;add++){
 '-----<
            tillatnaSiffror[i][ruta].add(add);
            System.out.println(tillatnaSiffror[i][ruta]);
         }

Ideone.com demo


As a side note, I would suggest to avoid arrays here, and use Java collections all the way. Consider for instance to use a structure like List<List<Set<Integer>>>.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文