Java – add elements to 2D ArrayList

I know that for arrays, you can add an element to a two-dimensional array as follows:

array[0][1] = 17; //just an example

How can I do the same thing with ArrayList?

Solution

myList.get(0).set(1,17);
myList.get(0).set(1,17);

Maybe?

This assumes a nested ArrayList, i.e

ArrayList<ArrayList<Integer>> myList;

And select the word you choose: This specifies a value for a specific location in the internal list, but does not add a value But so is your code example, because arrays have a fixed size, you must create them at the correct size and then assign values to each element slot

If you really want to add an element, of course it is Add (17), but this is not what your code does, so I chose the above code

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>