-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?

Community
  • 1
  • 1
Yitzak Hernandez
  • 355
  • 4
  • 23
  • If you're using at least Java 7, be sure to use the [diamond operator `<>`](http://stackoverflow.com/q/4166966/5743988) – 4castle Sep 04 '16 at 05:41

4 Answers4

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);
Abhishek
  • 379
  • 1
  • 8
0

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

ArrayList<ArrayList<String>> coordinates = new ArrayList<ArrayList<String>>();
Guy
  • 46,488
  • 10
  • 44
  • 88
0
  • like this : coordinates.addAll(buffer);
yux0210
  • 1
  • 2
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..

M.Usman
  • 2,049
  • 22
  • 25