Java - Adding a List <String> from a Parameter to an Existing List
I want to be able to add new parameter entry entries to the list.
For instance:
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 (element (from the parameter), when adding input to run this method) element that I need to add will be added to 'wholeList'.
As you can see, I have 3 items added to the list: salad, bacon and milk. However, if I suddenly change my mind and want to add another element (via a parameter), I can simply add it to the current list (wholeList).
And one more question.
Is there an easier way to add a list of items instead of adding it one by one (when using the same list import)? Say {Salad, Milk, Bacon, etc.}?
TY.
As I understand it, addAll() is all 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")); Well, your code is doing something very wrong. You initialize the wholeList inside the method, and after the method is finished, it disappears (pointers to Java). In addition, you added the list by itself, so the code is probably not the one you wanted to make.
you probably wanted to create a new list inside the method and add all the elements to the list in this parameter. If so, you should not use the βnewβ in the list that you received from the parameter.
Actually, after reading the title of your question -
- You need an existing list - it cannot be with the list name in the parameter. Let me call it an existingList.
- After you get the list in the method, you should not use the new ArralyList on it, since this will invalidate the list from this parameter.
Your code should look like this:
public static void theList (List<String> wholeList) { wholeList.add("Lettuce"); wholeList.add("Bacon"); wholeList.add("Milk"); existingList.add(wholeList); The only "cleaner" way to add values ββto the list would be:
wholelist.addAll(Arrays.asList("Lettuce", "Bacon", "Milk")); But I see that Top has already said in the answer. That way you can clear it even more by creating an array as a global private variable outside the method. In addition, as another answer said, you should have another separate list that does not have the same name as the parameter list. Here is an example with the required libraries:
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 want to use wholeList as the name for both lists, you can change globalList above to the whole list, and then:
public static void theList (List<String> wholelist) { this.wholelist.AddAll(wholelist); this.wholelist.addAll(Arrays.asList(list)); } But I would not do that.