Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中向arraylist动态添加更多值_Java_Android_Arraylist - Fatal编程技术网

在java中向arraylist动态添加更多值

在java中向arraylist动态添加更多值,java,android,arraylist,Java,Android,Arraylist,java中的数组和列表仍然有点新,我希望动态地将元素添加到列表中,例如,如果我有这样的代码: List<int[]> rowList = new ArrayList<int[]>(); rowList.add(new int[] { 1, 2, 3 }); rowList.add(new int[] { 4, 5, 6 }); rowList.add(new int[] { 7, 8 }); 如何动态添加1、2、3、4、5、6、7等?提前感谢您的帮助只需使用列表直接存

java中的数组和列表仍然有点新,我希望动态地将元素添加到列表中,例如,如果我有这样的代码:

List<int[]> rowList = new ArrayList<int[]>();

rowList.add(new int[] { 1, 2, 3 });
rowList.add(new int[] { 4, 5, 6 });
rowList.add(new int[] { 7, 8 });
如何动态添加1、2、3、4、5、6、7等?提前感谢您的帮助

只需使用列表直接存储号码即可:

 List<Integer> ints = new ArrayList<Integer>();
 ints.add(1);    // works with Java 1.5+, inboxing
 ints.add(2);
我们有:

 private int[] newValue(int a) {
    int[] result = new int[1];
    result[0] = a;
    return result;
 }
编辑

更多Java 1.5+魔力,使用varargs和自动装箱:

 private int[] newValue(Integer... values) {
    int[] result = new int[values.length];
    for (int i = 0; i < result.length; i++)
       result[i] = values[i];
    return result
 }
用法:


如果要向列表中添加新的int[],首先必须创建并填充它。如果你不知道数组的长度,我建议你使用一个数组,当你达到它的极限时,它会动态地增加空间


创建此数组后,只需将其作为参数传递。应该是直截了当的。

如果您想添加它们而不写出每个数字,可以使用for循环

for(int i = 0; i < [however many you would like]; i++) {
    ints.add(i);
}

如果您有JDK 5或更高版本,则可以使用这种效果

public class ListUtils {
    public static void add(List<int[]> list, int... args) {
        if (list != null && args != null) {
            list.add(args);
        }
    }
}

我不确定您需要什么,但我希望此示例将帮助您:

List<Integer> rowList = new ArrayList<Integer>();
Collections.addAll(rowList, ArrayUtils.toObject(new int[] { 1, 2, 3 }));
Collections.addAll(rowList, ArrayUtils.toObject(new int[] { 4, 5, 6 }));
Collections.addAll(rowList, ArrayUtils.toObject(new int[] { 7, 8 }));
ArraysUtils是ApacheCommons的一个类,非常适合处理原语和对象数组

问候,,
我不太明白。要保留int[]列表还是int的列表?当你说“动态添加1,2,3,4,5,6,7”时,你的意思是将它们作为一个数组添加还是将它们单独添加?你想添加另一个数组,还是向其中一个数组添加数字?可以像执行三次一样执行前者:rowList.addnew int[]{1,2,3,4,5,6,7,8};。后者无法实现,因为数组的长度一经创建就不可变。您的代码是有效的。它将把三个int数组添加到您的列表中。您不能将单个值添加到该列表中,因为它是一个int数组列表。请澄清你想要实现的目标。希望我理解你想要实现的目标;有趣的是,你是在我的解决方案之后还是之前介绍varargs的-解决方案@Andreas\u D的thanx
public class ListUtils {
    public static void add(List<int[]> list, int... args) {
        if (list != null && args != null) {
            list.add(args);
        }
    }
}
ListUtils.add(list, 1, 2, 3, 4, 5, 6, 7);
List<Integer> rowList = new ArrayList<Integer>();
Collections.addAll(rowList, ArrayUtils.toObject(new int[] { 1, 2, 3 }));
Collections.addAll(rowList, ArrayUtils.toObject(new int[] { 4, 5, 6 }));
Collections.addAll(rowList, ArrayUtils.toObject(new int[] { 7, 8 }));