Reputation: 1033
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 '.
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).
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.
Upvotes: 8
Views: 103101
Reputation: 393
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:
public static void theList (List<String> wholelist) {
this.wholelist.AddAll(wholelist);
this.wholelist.addAll(Arrays.asList(list));
}
But I would avoid doing that.
Upvotes: 1
Reputation: 213
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.
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.
Actually, after reading the title of your question -
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);
Upvotes: 5
Reputation: 413
As I understand, addAll()
is everything you need:
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"));
Upvotes: 10