Java 在android中将整数arraylist添加到arraylist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21936336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 11:29:17  来源:igfitidea点击:

Adding integer arraylist to arraylist in android

javaandroidarraylistinteger

提问by user3337408

I need to make a 2D arraylist like

我需要制作一个二维数组列表

[[1,2,3],[2,3,4],[3,4,5]]

But by using the below code I am getting only

但是通过使用下面的代码,我只得到

[[3,4,5],[3,4,5],[3,4,5]]

The last array simply gets repeated again and again. While traversing through the problem, I found that in first pass its like

最后一个数组只是一次又一次地重复。在遍历问题时,我发现在第一次通过时它就像

[[1,2,3]

2nd pass:

第二关:

[[2,3,4],[2,3,4]]

3rd pass:

第三关:

[[3,4,5],[3,4,5],[3,4,5]].

Please help, as i m stuck with it.

请帮助,因为我坚持它。

ArrayList<ArrayList<Integer>> combiarray = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> innerarray = new ArrayList<Integer>();

combiarray.clear();

for(k=1;k<(row*column);k=k+column)
{
    for(j=k;;j++)
        {
            innerarray.clear();
            for(i=0;i<wincount;i++)
            {
                innerarray.add(j+i);
            }

            combiarray.add(innerarray);
            Toast.makeText(context,String.valueOf(combiarray),
                                 Toast.LENGTH_SHORT).show();
            i--;
            if((j+i)%column==0)
            {
                break;
            }
        }
    }

采纳答案by Alexis C.

The problem is that you're always adding the same arraylist object to the list and you just modify its content. You have to create a new object at each iteration.

问题是您总是将相同的 arraylist 对象添加到列表中,而您只是修改了它的内容。您必须在每次迭代时创建一个新对象。

Currently you're doing something like this:

目前你正在做这样的事情:

enter image description here

在此处输入图片说明

So replace

所以更换

innerarray.clear();

by

经过

innerarray = new ArrayList<Integer>();

回答by Slimu

ArrayList class has a method called addAll that adds all the content from a collection to your collection. You can use it to add to your final ArrayList the content of the other lists.

ArrayList 类有一个名为 addAll 的方法,用于将集合中的所有内容添加到您的集合中。您可以使用它来将其他列表的内容添加到最终的 ArrayList 中。

If you wish to generate the lists in the required format, you can use:

如果您希望以所需格式生成列表,您可以使用:

 ArrayList<ArrayList<Integer>> combiArray = new ArrayList<ArrayList<Integer>>();
    int columns = 3;
    for (int i = 1; i <= columns; i++) {
        ArrayList<Integer> innerArray = new ArrayList<Integer>();
        for (int j = i; j < i + columns; j++) {
            innerArray.add(j);
        }
        combiArray.add(innerArray);
    }

回答by Anil kumar

try this once...

试试这个...

for(k=1;k<(row*column);k=k+column)
{
for(j=1;j<=k;j++)
    {
        innerarray.clear();
        for(i=0;i<wincount;i++)
        {
            innerarray.add(j+i);
        }

        combiarray.add(innerarray);
        Toast.makeText(context,String.valueOf(combiarray),Toast.LENGTH_SHORT).show();
        i--;
        if((j+i)%column==0)
        {
            break;
        }

    }
}