0

I am trying to up create and update an ArrayList by passing an argument, so that I will end up with a list of say 10 names; however, the current function doesn't seem to be working - any ideas pls?

   public String addClient(String name) {
     ArrayList<String> myList = new ArrayList<String>();
     myList.add(name);

     return myList;
   }
2
  • public String addClient(String name) must return a String, not a List<>. Commented Nov 21, 2016 at 20:49
  • You have a method having return type String and you are returning ArrayList? Commented Nov 21, 2016 at 20:50

3 Answers 3

3

You are creating a new ArrayList every time you call it. This means that every time you call this method you create a brand new Collection and only store the one client in it. You need to keep a reference of a single collection around and keep adding to that. You can do that by passing in the array you want to add it to:

public List<String> addClient(String name, List<String> array) {
    array.add(name);
    return array;
}

This doesn't seem like a useful function, so I'm guessing this is within a class. So this might be the approach you want:

/**
 * Class is not Thread Safe
 */
public class ClientList {
    private final ArrayList<string> clients;

    public ClientList() {
        this.clients = new ArrayList<>();
    }

    public void addClient(String client) {
        this.clients.add(client);
    }

    public List<String> getClients() {
        // Note: Never give a reference to the internal objects of the class
        // as that means someone outside this class can own a reference to it
        // and can update the object without you knowing (by not going
        // through this class)
        Collections.unmodifiableList(this.clients);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

and return List<String>
Updated. Though that seems like a very weird method - unless you are doing a Builder pattern...
that ClientList class looks like a valid solution to me but also if we go further and wanted to make it a threadSafe one then we'd have to do it abit differently. :)
@SergeyBenner totally :) But that was outside the scope of the question :) I'll add a note that it isn't thread safe (for future Googlers)
0

This is what you need to do:

ArrayList<String> myList = new ArrayList<String>();

public void addClient(String name) {
    myList.add(name);
}

If you create a list inside the method, it will only have one value, and will go away once method execution finishes (unless it's returned). Have a look at different scopes here. You should create a list at a class level and add the elements into it.

Also, method does not need to return anything, so it's better to change the type to void.

Comments

0

The problem with your approach is that everytime you call the method addClient a new ArrayList will be created. I think this will work for you :

static ArrayList<String> myList;

public static void main(String[] args) {
    myList = new ArrayList<>();
}

public void addClient(String name){
    myList.add(name);             
}

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.