0

I wrote this code to add arraylist to my 2x2 arraylist

ArrayList<ArrayList<String>> addressesAndCores = new ArrayList<ArrayList<String>>();
addressesAndCores.add((new ArrayList<String>().add(remoteIp.getText())));
addressesAndCores.add((new ArrayList<String>().add(remoteIp2.getText())));

However Eclipse gives me the error:

The method add(ArrayList<String>) in the type ArrayList<ArrayList<String>> is not applicable for the arguments (boolean)

It recommends changing add to addall but when I do so it throws this error:

The method addAll(Collection<? extends ArrayList<String>>) in the type ArrayList<ArrayList<String>> is not applicable for the arguments (boolean)

And recommends I change it to add...

Any help would be greatly appreciated

4
  • I get this a lot too with similar containers within containers, but in NetBeans! In the end I got so fed up of the 'warnings' I removed the types and stuck with generic objects. Works for me. Commented Aug 2, 2013 at 16:02
  • @iaindownie This is an error not a warning. Commented Aug 2, 2013 at 16:03
  • 2
    Why are you complicating yourself, doing everything in single line, which got you into compiler error. You do it step by step, and everything would seem easy. Commented Aug 2, 2013 at 16:03
  • Don't forget to look at the javadoc when something isn't working as expected. You could have easily found out that add() returns a boolean and not the ArrayList. Commented Aug 2, 2013 at 16:07

3 Answers 3

4

The problem is that the add method of ArrayList does not return the instance (the opposite would be StringBuilder's append which does return the instance).

The method ArrayList.add will return true if the Collection has changed after performing add.

Therefore you are actually adding boolean to addressesAndCores.

Instead, you can use:

ArrayList<String> toAdd = new ArrayList<String>();
toAdd.add(remoteIp.getText());
addressesAndCores.add(toAdd);

More documentation here:

  • ArrayList add method
  • Collection add method
  • StringBuilder doc
Sign up to request clarification or add additional context in comments.

2 Comments

That's it, but add() returns boolean, not void, hence the error "is not applicable for the arguments (boolean)"
that's not ok... cause addressesAndCores expects ArrayList not List
0

what happened,

you created your list,then you are adding to this list your text new ArrayList<String>().add(remoteIp.getText()) then you are adding to your main list result of this operation instead your list,

and as add() returns boolean and your list expecting ArrayList<String> you have a type mismatch

Comments

0

You cannot do that as several others have pointed out.

I would like to suggest that you create your own builder:

public class ExampleBuilder {

    public static ExampleBuilder newTwoDListBuilder() {
        return new ExampleBuilder();
    }
    private final List<List<String>> underlyingList = new ArrayList<>();

    public ExampleBuilder() {
    }

    public ExampleBuilder add(final List<String> strings) {
        underlyingList.add(strings);
        return this;
    }

    public ExampleBuilder add(final String... strings) {
        underlyingList.add(Arrays.asList(strings));
        return this;
    }

    public List<List<String>> build() {
        return new ArrayList<>(underlyingList);
    }
}

You can then import static it and use it like this:

public static void main(String[] args) throws Exception {
    final List<List<String>> list = newTwoDListBuilder().add("ONE").add("TWO").build();
    System.out.println(list);
}

Output

[[ONE], [TWO]]

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.