Java 将多个字符串变量添加到 ArrayList

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24922011/
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-14 15:45:07  来源:igfitidea点击:

Add multiple String variables to ArrayList

javastringarraylist

提问by CSnerd

Suppose I have a lot of String Variables(100 for example):

假设我有很多字符串变量(例如 100):

   String str1 = "abc";
    String str2 = "123";
    String str3 = "aaa";
....
    String str100 = "zzz";

I want to add these String variables to ArrayList, what I am doing now is

我想将这些字符串变量添加到 ArrayList,我现在正在做的是

    ArrayList<String> list = new ArrayList<String>();
    list.add(str1);
    list.add(str2);
    list.add(str3);
...
    list.add(str100);

I am curious, is there a way to use a loop? For example.

我很好奇,有没有办法使用循环?例如。

for(int i =  1; i <= 100; i++){
     list.add(str+i)//something like this?
}

采纳答案by markspace

Use an array:

使用数组:

String[] strs = { "abc","123","zzz" };

for(int i =  0; i < strs.length; i++){
     list.add(strs[i]);  //something like this?
}

This idea is so popular that there's built-in methods to do it. For example:

这个想法非常流行,以至于有内置的方法可以做到这一点。例如:

  list.addAll( Arrays.asList(strs) );

will add your array elements to an existing list. If you just want a list with only the array elements, you can do it on one line:

将您的数组元素添加到现有列表中。如果你只想要一个只有数组元素的列表,你可以在一行上完成:

  List<String> list = Arrays.asList( strs );

回答by Pshemo

If strXwould be class fields then you could try using reflection - linkto example of accessing fields and methods.

如果strX是类字段,那么您可以尝试使用反射 -链接到访问字段和方法的示例。

If it is local variable then you can't get access to its name so you will not be able to do it (unless strwould be array, so you could access its values via str[i]but then you probably wouldn't need ArrayList).

如果它是局部变量,那么您将无法访问其名称,因此您将无法执行此操作(除非str是数组,因此您可以通过访问其值,str[i]但您可能不需要ArrayList)。

Update:

更新:

After you updated question and showed that you have 100 variables

在您更新问题并显示您有 100 个变量之后

String str1 = "abc";
String str2 = "123";
String str3 = "aaa";
//...
String str100 = "zzz";

I must say that you need array. Arrays ware introduced to programming languages precisely to avoid situation you are in now. So instead of declaring 100 separate variables you should use

我必须说你需要 array。将数组引入编程语言正是为了避免您现在所处的情况。因此,您应该使用而不是声明 100 个单独的变量

String[] str = {"abc", "123", "aaa", ... , "zzz"};

and then access values via str[index]where index is value between 0and size of your array -1, which in you case would be range 0- 99.

然后通过str[index]where index is value between 0and size of your array -1 来访问值,在你的情况下是 range 0- 99



If you would still would need to put all array elements to list you could use

如果您仍然需要将所有数组元素放在列表中,您可以使用

List<String> elements = new ArrayList<>(Arrays.asList(str));

which would first

哪个会先

Arrays.asList(str)

create list backed up by strarray (this means that if you do any changes to array it will be reflected in list, and vice-versa, changes done to list from this method would affect strarray).

创建由str数组备份的列表(这意味着如果您对数组进行任何更改,它将反映在列表中,反之亦然,从此方法对列表所做的更改将影响str数组)。

To avoid making list dependant on state of array we can create separate list which would copy elements from earlier list to its own array. We can simply do it by using constructor

为了避免列表依赖于数组的状态,我们可以创建单独的列表,它将元素从早期列表复制到它自己的数组。我们可以简单地通过使用构造函数来做到这一点

new ArrayList<>(Arrays.asList(str));

or we can separate these steps more with

或者我们可以更多地将这些步骤分开

List<String> elements = new ArrayList<>();//empty list
elements.addAll(Arrays.asList(str));//copy all elements from one list to another

回答by ajb

Yes. The way to use a loop is not to declare 100 string variables. Use one array instead.

是的。使用循环的方法不是声明 100 个字符串变量。请改用一个数组。

String[] str = new String[101];
str[1] = "abc";
str[2] = "123";
str[3] = "aaa";
....
str[100] = "zzz";

(I made the indexes go from 1 to 100 to show how it corresponds to your original code, but it's more normal to go from 0 to 99 instead, and to initialize it with an array initializer as in @markspace's answer.)

(我使索引从 1 变为 100 以显示它如何对应于您的原始代码,但从 0 变为 99 更正常,并使用数组初始值设定项对其进行初始化,如@markspace 的答案。)

回答by AlexR

If you want to stick to good practice, declare your Strings in an array:

如果您想坚持良好的做法,请String在数组中声明您的s:

String[] strs = new String[]{ "abc", "123", "aaa", ... };
for (String s : strs) // Goes through all entries of strs in ascending index order (foreach over array)
    list.add(s);

回答by Noyal

If you are using Java 9 then easily you can add the multiple String Objects into Array List Like

如果您使用的是 Java 9,那么您可以轻松地将多个字符串对象添加到数组列表中,例如

List<String> strings = List.of("abc","123","zzz");

回答by xavierz

The following creates the ArrayListon the specific Stringvalues you have:

以下创建了您拥有ArrayList的特定String值:

ArrayList<String> list1 = new ArrayList<String>() {{addAll(Arrays.asList(new String[]{"99", "bb", "zz"}));}};

Or, if it's just some distinct values you want, use this for say - 10 of them:

或者,如果它只是您想要的一些不同的值,请使用它来表示 - 其中 10 个:

ArrayList<String> list2 = new ArrayList<String>() {{for (int i=0; i<10; i++) add(""+System.currentTimeMillis());}};