8

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.

1
  • Even if I don't understand your problem, what I have to say about your snippet pasted is that you can't add to a List the List itself (via the addAll method). Commented Mar 17, 2013 at 12:22

3 Answers 3

10

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"));
Sign up to request clarification or add additional context in comments.

1 Comment

If I read correctly between the lines of the question, it might be that you have to remove already existing items from the itemsToAdd list, 'cause he wants only additional items being added. This could be done in a simple loop.
5

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 -

  1. You need an existing list - it can't be with the name of the list in the parameter. Let's call it existingList.
  2. After you get the list in the method, you shouldn't use the "new ArralyList" on it, as it will void the list from the parameter.

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);

1 Comment

I understood the problem. Think my questioning is wrong. But I meant adding a new element when I run the method. E.g. I have the list, and when I run the method I can add new elements to existing list.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.