简体   繁体   English

将多个项目添加到 Java 中已初始化的数组列表

[英]Add multiple items to an already initialized arraylist in Java

My arraylist might be populated differently based on a user setting, so I've initialized it with我的arraylist可能会根据用户设置以不同的方式填充,因此我已将其初始化为

ArrayList<Integer> arList = new ArrayList<Integer>();

How can I add hundreds of integers without doing it one by one with arList.add(55);如何在不使用arList.add(55); ? ?

If you have another list that contains all the items you would like to add you can do arList.addAll(otherList) .如果您有另一个列表,其中包含您想要添加的所有项目,您可以执行arList.addAll(otherList) Alternatively, if you will always add the same elements to the list you could create a new list that is initialized to contain all your values and use the addAll() method, with something like或者,如果您总是将相同的元素添加到列表中,您可以创建一个新列表,该列表已初始化为包含所有值并使用addAll()方法,例如

Integer[] otherList = new Integer[] {1, 2, 3, 4, 5};
arList.addAll(Arrays.asList(otherList));

or, if you don't want to create that unnecessary array:或者,如果您不想创建那个不必要的数组:

arList.addAll(Arrays.asList(1, 2, 3, 4, 5));

Otherwise you will have to have some sort of loop that adds the values to the list individually.否则,您将不得不有某种循环将值单独添加到列表中。

What is the "source" of those integers?这些整数的“来源”是什么? If it is something that you need to hard code in your source code, you may do如果您需要在源代码中硬编码,您可以这样做

arList.addAll(Arrays.asList(1,1,2,3,5,8,13,21));

Collections.addAll is a varargs method which allows us to add any number of items to a collection in a single statement: Collections.addAll是一个可变参数方法,它允许我们在单个语句中将任意数量的项目添加到集合中:

List<Integer> list = new ArrayList<>();
Collections.addAll(list, 1, 2, 3, 4, 5);

It can also be used to add array elements to a collection:它还可以用于将数组元素添加到集合中:

Integer[] arr = ...;
Collections.addAll(list, arr);

If you are looking to avoid multiple code lines to save space, maybe this syntax could be useful:如果您希望避免使用多行代码以节省空间,那么这种语法可能会很有用:

        java.util.ArrayList lisFieldNames = new ArrayList() {
            {
                add("value1"); 
                add("value2");
            }
        };

Removing new lines, you can show it compressed as:删除新行,您可以将其压缩为:

        java.util.ArrayList lisFieldNames = new ArrayList() {
            {
                add("value1"); add("value2"); (...);
            }
        };

Java 9 + now allows this: Java 9 + 现在允许这样做:

List<Integer> arList = List.of(1,2,3,4,5);

The list will be immutable though.该列表将是不可变的。

If you needed to add a lot of integers it'd probably be easiest to use a for loop.如果你需要添加很多整数,使用for循环可能是最简单的。 For example, adding 28 days to a daysInFebruary array.例如,将 28 天添加到 daysInFebruary 数组。

ArrayList<Integer> daysInFebruary = new ArrayList<>();

for(int i = 1; i <= 28; i++) {
    daysInFebruary.add(i);
}

In a Kotlin way;Kotlin的方式;

val arList = ArrayList<String>()
arList.addAll(listOf(1,2,3,4,5))

I believe scaevity's answer is incorrect.我相信scaevity 的回答是不正确的。 The proper way to initialize with multiple values would be this...用多个值初始化的正确方法是......

int[] otherList = {1,2,3,4,5};

So the full answer with the proper initialization would look like this所以正确初始化的完整答案看起来像这样

int[] otherList = {1,2,3,4,5};
arList.addAll(Arrays.asList(otherList));

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

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