ArrayList of arrays Java

ArrayList of arrays Java

Sometimes it may be necessary to store arrays in ArayList in coding. It is also necessary to retrieve the elements of the arrays when required. Another similar concept program is available "Array of Array Lists" that stores array lists in an array of array list.

Example on ArrayList of arrays


ArrayList of arrays
Output screen of ArrayList of arrays

A generics ArrayList al1 is created that stores arrays. Object array is chosen as it permits to add any type of array.

The al1 is added with arrays of String, Integer and Double.

With enhanced for loop, the elements of all the arrays (names, marks and heights) are printed. toStriing() method of Arrays is used to print the elements of each array. toString() method avoids looping.

Alternatively, a generics ArrayList to store string arrays can be taken and added with only string arrays as follows.

Read also Array of Array Lists.

Leave a Reply

Your email address will not be published. Required fields are marked *