Java - 将 List<String> 从参数添加到现有列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15460566/
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
Java - Adding List<String> from parameter to existing list
提问by Dembele
I want to be able to add new entries of parameter inputs to the list.
我希望能够将参数输入的新条目添加到列表中。
For example:
例如:
public static void theList (List<String> wholeList) {
wholeList = new ArrayList<String>();
wholeList.add("Lettuce");
wholeList.add("Bacon");
wholeList.add("Milk");
wholeList.add(wholeList); <--------- error - addAll doesn't fix it.
Above I tried ' wholeList.add(wholeList) '. What I intended to do is: Whatever additional (item (from parameter), when adding the input to run this method) item I need to add, will be added to the ' wholeList '.
上面我试过' WholeList.add(wholeList) '。我打算做的是:我需要添加的任何其他(项目(来自参数),在添加输入以运行此方法时)项目都将添加到“整体列表”中。
As you can see, I have 3 items added to the list: Lettuce, Bacon and Milk. However, if I suddenly changed my mind, and want to add another item (via parameter), I can simply add it to the current list (wholeList).
如您所见,我在列表中添加了 3 个项目:生菜、培根和牛奶。但是,如果我突然改变主意,想要添加另一个项目(通过参数),我可以简单地将它添加到当前列表(wholeList)。
Also, another question.
另外,还有一个问题。
Is there a neater way to add a list of items instead of adding it one-by-one (whilst using the same list import)? Say, {"Lettuce", "Milk", "Bacon", etc}?
有没有更简洁的方法来添加项目列表而不是一个一个地添加它(同时使用相同的列表导入)?说,{“生菜”、“牛奶”、“培根”等}?
TY.
泰。
回答by Ivan Borisov
As I understand, addAll()
is everything you need:
据我了解,addAll()
是您需要的一切:
List<String> someList = new ArrayList<String>();
List<String> itemsToAdd = new ArrayList<String>();
itemsToAdd.add("one");
itemsToAdd.add("two");
someList.addAll(itemsToAdd);
// or use handy method which creates temporary list internally:
someList.addAll(Arrays.asList("three", "four"));
回答by Issahar Weiss
Well, your code does something very wrong. You initialize the wholeList inside the method, and after the method is finished, it is gone (pointers in Java). Also, you added the list on itself, so the code is probably not what you wanted to do.
好吧,你的代码做了一些非常错误的事情。你在方法内部初始化了整个列表,方法完成后,它就消失了(Java 中的指针)。此外,您添加了列表本身,因此代码可能不是您想要做的。
you probably meant to create a new list inside the method and add all the items to the list in the parameter. If so, you shouldn't use "new" on the list that you got from a parameter.
您可能打算在方法中创建一个新列表并将所有项目添加到参数中的列表中。如果是这样,您不应该在从参数获得的列表中使用“new”。
Actually, after reading the title of your question -
实际上,在阅读了您的问题标题后-
- You need an existing list - it can't be with the name of the list in the parameter. Let's call it existingList.
- After you get the list in the method, you shouldn'tuse the "new ArralyList" on it, as it will void the list from the parameter.
- 您需要一个现有列表 - 它不能与参数中的列表名称一起使用。我们称之为existingList。
- 在方法中获取列表后,不应在其上使用“new ArralyList”,因为它会使参数中的列表无效。
Your code should look like that:
您的代码应如下所示:
public static void theList (List<String> wholeList) {
wholeList.add("Lettuce");
wholeList.add("Bacon");
wholeList.add("Milk");
existingList.add(wholeList);
回答by Cody Ferguson
The only "cleaner" way of adding the values to the list would be:
将值添加到列表的唯一“更干净”的方法是:
wholelist.addAll(Arrays.asList("Lettuce", "Bacon", "Milk"));
But I see the Top answer already states that. So, you could clean it up more by creating a array as a global private variable outside of the method. Also, as another answer said, you should have another seperate list that does not share the same name as the parameter list. Here is an example with libaries needed:
但我看到顶级答案已经说明了这一点。因此,您可以通过在方法之外创建一个数组作为全局私有变量来进一步清理它。此外,正如另一个答案所说,您应该有另一个与参数列表不共享相同名称的单独列表。这是一个需要库的示例:
import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
public class Example{
private List<String> globalList = new ArrayList<>();
private String[] list = {"Bacon", "Lettuce", "Milk"};
public static void theList (List<String> wholelist) {
wholelist.addAll(Arrays.asList(list));
globalList.addAll(wholeList);
}
If you wanted to use wholeList as the name for both lists, then you could change globalList above to wholelist, then:
如果您想使用 WholeList 作为两个列表的名称,那么您可以将上面的 globalList 更改为 Wholelist,然后:
public static void theList (List<String> wholelist) {
this.wholelist.AddAll(wholelist);
this.wholelist.addAll(Arrays.asList(list));
}
But I would avoid doing that.
但我会避免这样做。