简体   繁体   English

动态地向java中的arraylist添加更多值

[英]dynamically adding more values to an arraylist in java

still a bit new with arrays and lists in java, I would like add elements to a list dynamically, so for example if I had code like this: 对于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 });

how would I dynamically add 1,2,3,4,5,6,7 etc? 我将如何动态添加1,2,3,4,5,6,7等? thanks in advance for the help 先谢谢您的帮助

Simply use a List<Integer> to store the numbers directly: 只需使用List<Integer>直接存储数字:

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

Or, if you want to keep your datastructure, wrap the numbers in short arrays: 或者,如果要保留数据结构,请将数字包装在短数组中:

 rowList.add(newValue(1));

where we have: 我们在哪里:

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

Edit 编辑

Some more Java 1.5+ magic, using varargs and autoboxing: 使用varargs和autoboxing的Java 1.5+魔法:

 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
 }

Usage: 用法:

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

rowList.add(newValues(10, 20, 30));
rowList.add(newValues(1,2,3,4,5,6,7));

If you want to add a new int[] to the list you will first have to create and fill it. 如果要在列表中添加新的int [],首先必须创建并填充它。 If you don't know the length the array will be I would suggest an array that will dynamically add space as your reach its limit. 如果你不知道数组的长度,我会建议一个数组,当你达到它的极限时会动态地增加空间。

After you have created this array, just pass it as an argument. 创建此数组后,只需将其作为参数传递。 Should be straight forward. 应该是直截了当的。

You could use a for loop if you would like to add them without writing out every single number. 如果您想在不写出每个数字的情况下添加它们,可以使用for循环。

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

Something of this effect (using varargs if you have JDK 5 and higher). 这种效果的一些东西(如果你有JDK 5或更高版本,使用varargs )。

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

Now, you can do 现在,你可以做到

ListUtils.add(list, 1, 2, 3, 4, 5, 6, 7);

I am not sure of what you need but I hope this example will help you : 我不确定你需要什么,但我希望这个例子可以帮助你:

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 is a class of apache commons really usefull to deal with primitive and object arrays. ArraysUtils是一类非常有用的apache公共文件来处理原始和对象数组。

Regards, Diodfr 此致,Diodfr

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

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