-1

So I'm trying to create a 2d ArrayList but I'm having some trouble adding one list to another. I found this question that seemed to answer my question but when I try it out myself I get a red warning squiggle under the last add on coordinates.add()

Here's my code

ArrayList<String> coordinates = new ArrayList<String>();
ArrayList<String> buffer = new ArrayList<String>();
buffer.add("123");
buffer.add("abc");

coordinates.add(buffer);

What am I doing wrong here?

1

4 Answers 4

2

you created two arraylist of string name coordinates and buffer. so you can not add one arraylist into a arraylist of string . if you want to add a array list into another then try following code

   ArrayList<ArrayList<String>> coordinates = new ArrayList<ArrayList<String>>();
  ArrayList<String> buffer = new ArrayList<String>();
  buffer.add("123");
  buffer.add("abc");

  coordinates.add(buffer);
Sign up to request clarification or add additional context in comments.

1 Comment

Im tired and its late, I had it set to ArrayList<ArrayList<String>>and I must of changed it and forgot to put it back.. thanks man
0

coordinates should be of type ArrayList<ArrayList<String>>

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

Comments

0
  • like this : coordinates.addAll(buffer);

1 Comment

Please add description and more detail to the answer just for helps more
0

Just use addAll method Instead of using add like below:

ArrayList<String> coordinates = new ArrayList<String>();
ArrayList<String> buffer = new ArrayList<String>();
buffer.add("123");
buffer.add("abc");

coordinates.addAll(buffer);

it will not give any error or warning.. thank you..

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.