1

I am just trying to be a little more familiar with arrayList and made a test program. I just want to add data into an arrayList of another class. It says that "The method add(arrayList) in the type Test is not applicable for the arguments (String)"

 package arrayList;

import java.util.ArrayList;
import java.util.List;

public class arrayList 
{

    public static void main(String[] args) throws Exception
    {
    ArrayList<String> cityList = new ArrayList<String>();
    // Add some cities in the list

    Test add = new Test();

    add.add("Dallas");

    System.out.print(cityList);
}
}

Here is the class that adds data into the arrayList package arrayList;

import java.util.ArrayList;
import java.util.List;

public class Test 
{
    public final ArrayList<arrayList> floors = new ArrayList<>();

    public void add(arrayList cityName)
    {
    floors.add(cityName);
    }
}
3
  • 1
    What's the question? Commented Oct 22, 2016 at 15:34
  • @DaveNewton, sorry I edited it. It's a compiler error. Commented Oct 22, 2016 at 15:35
  • do you want to add to the floors array or the cityList array? Commented Oct 22, 2016 at 15:43

1 Answer 1

1

"The method add(arrayList) in the type Test is not applicable for the arguments (String)"

Simply because you try to add a String to an ArrayList<arrayList> while only objects of type arrayList are expected.

Change your code as next to be able to add String to your list:

// My list of String
private final List<String> floors = new ArrayList<>();

public void add(String cityName) {
    // Add a String to my list of String
    floors.add(cityName);
}

If you want to access to your list from outside the class, you could add a getter to your class Test, like this:

public List<String> getFloors() {
    return floors;
}

If you want to initialize your list from a list created outside your class, you could add a constructor to your class Test, like this:

private final List<String> floors;

public Test(List<String> floors) {
    this.floors = floors;
}

NB: As you are a beginner I provided a simple answer but please note that it is not a good practice to use directly a list coming from outside your class like in the constructor above or returning directly your list like in the getter above. A good practice would be to protect your list from outside classes to prevent bugs by making a copy of the provided list in case of the constructor and by returning a copy of your list or the unmodifiable version of your list in case of a getter.

Sign up to request clarification or add additional context in comments.

6 Comments

How should I change it to where it accepts the data from the other class?
I'm not sure I understand your question, could your clarify please?
That is just creating a new arrayList within the Test class and not my actual arrayList class, isn't it?
your ArrayList and the one in Test are two different instances so different lists
It is not good programming style to expose a collection to the rest of the program in order to make changes on the list. Usually one exposes only an iterator or an unmodifiable List ("Collections.unmodifiableList( myList)"). So OPs add method is OK.
|

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.