Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

adding objects to a 2D ArrayList  RSS feed

 
Marius Semeon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm having trouble adding objects to a 2D ArrayList, and am also a little confused about the different ways of declaring and initializing 2D ArrayLists and their contents.

I am familiar with this way of declaring a 2D list:


But I didn't realise you can also declare it like you would a 2D object or primitive int array:

And then construct a 2D Array like:


But I don't get how you then elements to the ArrayLists within the list (i.e to the inner ArrayList), for example to the List at arr[2][3] specifically, or anywhere else.

Any help would be appreciated. Perhaps I am doing something fundamentally wrong.


 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedantic mode: there is no such thing as a 2D array or list. All you have is an array of arrays or a list of lists.

Your array has the wrong inner type. Your List<List<String>> is a List of Lists of String. The array equivalent is an array of arrays of String: String[][].
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a list of  lists of Strings. To add a String...

This is an array of arrays of lists of Strings. To add a String...

 
Marius Semeon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guy's I think I understand. But in your example, to add a single inner list to the outer one you had to individually declare each innerList and then individually add them:


I know you could repeatedly do this and then give them the names "innerList1", "innerList2", "innerList3" and so on, so you could easily add Strings to each specific innerList, but what if you wanted to have many (possibly thousands) of innerLists. Is it not possible to use something like the nested for loop


I realise that in that bit of code it's simply adding the same innerList (called simply "innerList") to each position of outerArray, but since it's not possible in Java to dynamically name new Objects (like after creating a new innerList you could name it depending on the current value of i and j) I don't understand how you could add to specific innerLists.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure you have got the concept of the relationship between Lists and Arrays. Because of peculiarities in generics, you cannot create an array of Lists of Strings as you showed (there are some fiddles you can employ depending on how strong your conscience is, however ‍).
So you have declared an array of arrays (as Rob told you, it isn't a 2D array), and that is its type. If it is a String[][] , it remains a String[][] forever. (In some cases you can squeeze subtype instances into that array, but String hasn't got any strict subtypes.) That means that myArray[i] has to be of type String[], and myArray[i][j] has to be of type String. You can't change that; Java® is a strongly typed language. You might be able to do that in a weakly typed language, but wait until you try to use the contents of the array and see what sort of errors you get at runtime. It is better to find out about the errors at compile time.
You can create an array: String[] inner1 = {"word1" ,"word2"}; and you can then assign that array to one of the elements of your String[][], but inner1 isn't a good name for most things. You have to assign that array as an element of an array whose type is String[][], and that is that.

Lists have changed; when I started Java®, they contained Object references and nothing else. If I wanted to add a primitive, I had to wrap it in a wrapper object:-
myList.add(new Integer(123));
All was well until we tried to get things back from that list:-
Integer i = (Integer)myList.get(0);
What if it wasn't an Integer? That cast will cause my app to crash. I am sure there were lots of complaints before generics was introduced in 2004 (it was actually planned much earlier than 2004). Now that you have declared your List as type List of Strings, it means you can only add Lists of Strings to that List. Carey has shown you how you can add things to ordinary arrays or arrays. It is similar for Lists of Lists:-You can be confident that the List contains Lists of Strings. You can be confident that the contents of the included Lists are Strings. No problem. You don't need any casts any more (look at my line 6). You have no risk of them causing your program to malfunction. But you have constrained yourself by that declaration as to what types of element you can or can't put into that List. You can't get the wrong type out because the compiler checks carefully that you aren't putting the wrong types in.

You may suffer errors at runtime if the sizes of the Lists are not right for the indices you are using, but that is a different problem.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...you cannot create an array of Lists of Strings as you showed...

I'm not sure what you are referring to. Here's code that compiles and runs:

Now whether or not this is what the OP intended I can't say.
 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are declaring a List<List<String>>[] but creating a List[], which is equivalent to using a raw type. I think this won't compile:-
List<List<String>>[] page = new List<List<String>>[123];
 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, my apologies. Nobody showed code like that which wouldn't compile.
 
Piet Souris
Rancher
Posts: 1981
67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:Pedantic mode: there is no such thing as a 2D array or list. All you have is an array of arrays (...)

Can't wait for the day when an OP talks about a 10D array!
But maybe an acceptable compromise is: an (N)D array is an array of (N-1)D arrays.   
 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:. . . maybe an acceptable compromise is: an (N)D array is an array of (N-1)D arrays.   
No it isn't; the only compromise is that your fingers fall of while writing array of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays of arrays ...

















No, it is easy with copy'n'paste
 
Marius Semeon
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Now whether or not this is what the OP intended I can't say.

This is basically what I wanted to do yes, except I'd like to create a large number of innerLists using for loops and then be able to add objects (or Strings, as in these examples) to a specific list at an indexed position.

But another problem is arising now: your code won't compile for me. I can't declare a new array of array of ArrayList like you did

I get "Recompile with -Xlint:unchecked for details" error message, and after recompiling with -Xlint:unchecked i get:
"warning: [unchecked] unchecked conversion.
  required: ArrayList<String>[][]
  found:    ArrayList[][]"
So I presume it means for me to declare it as but if I attempt to compile this I get "error: generic array creation"

This is driving me nuts; could this perhaps be because we have different versions of java? mine is 1.8.0_121.

 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Semeon wrote:. . . I'd like to create a large number of innerLists using for loops and then be able to add objects (or Strings, as in these examples) to a specific list at an indexed position.
That sounds easy enoughEasy‑peasy, as the children say round here. The only thing is, you will have to ensure there are enough elements in List₉₉₉ that you don't get an out of bounds exception.
But another problem is arising now: your code won't compile for me. . . . Recompile with -Xlint:unchecked for details . . .
The first problem is not an error but a warning. It says you are permitted the code, but you shou‍ld check that you are maintaining type‑safety.
error: generic array creation . . .
The second is an error: you cannot mix arrays and generics like that.
could this perhaps be because we have different versions of java? . . .
No.

So what is the answer? Easy. Do what you said you were going to do. Create multiple Lists. Forget about arrays. But remember that if you go round doing things like
myArray[123] = "CodeRanch";
and
myList.add(123, "CodeRanch");
you will get different results. The assignment to an array is more similar to the List#set method than add. In both cases you must ensure there are enough elements to accommodate the index you are using.

And as I said before: forget about arrays.
 
Carey Brown
Bartender
Posts: 2996
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marius Semeon wrote:
Carey Brown wrote:
Now whether or not this is what the OP intended I can't say.

This is basically what I wanted to do yes, except I'd like to create a large number of innerLists using for loops and then be able to add objects (or Strings, as in these examples) to a specific list at an indexed position.

But another problem is arising now: your code won't compile for me. I can't declare a new array of array of ArrayList like you did

I get "Recompile with -Xlint:unchecked for details" error message, and after recompiling with -Xlint:unchecked i get:
"warning: [unchecked] unchecked conversion.
  required: ArrayList<String>[][]
  found:    ArrayList[][]"
So I presume it means for me to declare it as but if I attempt to compile this I get "error: generic array creation"

This is driving me nuts; could this perhaps be because we have different versions of java? mine is 1.8.0_121.


Try

 
Campbell Ritchie
Marshal
Posts: 55747
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get suspicious whenever I see SuppressWarnings←link (though I do use it myself). I still would prefer not to use arrays at all. That array can be used more like a List<List<List<String>>>, I think.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!