Array and List in Java

Today we will learn how to create a Java array of ArrayList. We will also learn how to create an ArrayList of array elements.

Java Array of ArrayList

Creating array of list in java is not complex. Below is a simple program showing java Array of ArrayList example.

import java.util.ArrayList; import java.util.List; public class JavaArrayOfArrayList { public static void main[String[] args] { List l1 = new ArrayList[]; l1.add["1"]; l1.add["2"]; List l2 = new ArrayList[]; l2.add["3"]; l2.add["4"]; l2.add["5"]; List[] arrayOfList = new List[2]; arrayOfList[0] = l1; arrayOfList[1] = l2; for [int i = 0; i < arrayOfList.length; i++] { List l = arrayOfList[i]; System.out.println[l]; } } }

Notice that we cant use generics while creating the array because java doesnt support generic array. So if we try to use below code, it will produce compile time error as Cannot create a generic array of List.

List[] arrayOfList = new List[2];

Java ArrayList of Array

We can also create an array whose elements are a list. Below is a simple example showing how to create a list of array elements in java.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class JavaArrayListOfStringArray { public static void main[String[] args] { // List of String arrays List list = new ArrayList[]; String[] arr1 = { "a", "b", "c" }; String[] arr2 = { "1", "2", "3", "4" }; list.add[arr1]; list.add[arr2]; // printing list of String arrays in the ArrayList for [String[] strArr : list] { System.out.println[Arrays.toString[strArr]]; } } }

Java ArrayList of Object Array

If you are not sure about the type of objects in the array or you want to create an ArrayList of arrays that can hold multiple types, then you can create an ArrayList of an object array.

Below is a simple example showing how to create ArrayList of object arrays in java.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class JavaArrayListOfObjectArray { public static void main[String[] args] { // list of Object arrays to hold different types of array List list = new ArrayList[]; String[] arr1 = { "a", "b", "c" }; String[] arr2 = { "1", "2", "3", "4" }; JavaArrayListOfObjectArray aa = new JavaArrayListOfObjectArray[]; JavaArrayListOfObjectArray.A[] arr3 = { aa.new A["AA"], aa.new A["BB"] }; list.add[arr1]; list.add[arr2]; list.add[arr3]; // list holds different types of Object arrays, let's print them for [Object[] objArr : list] { System.out.println[Arrays.toString[objArr]]; // iterating over the array and doing operation based on it's type for [Object obj : objArr] { // using instanceof keyword to find the Object type in the array if [obj instanceof String] { // doSomethingForStringObject[]; } else if [obj instanceof JavaArrayListOfObjectArray.A] { // doSomethingForAObject[]; } } } } /** * A sample class to use in arraylist of arrays * * @author pankaj * */ public class A { private String name; public A[String n] { this.name = n; } public String getName[] { return this.name; } @Override public String toString[] { return "A.class::"+this.name; } } }

When we execute the above program, it produces the following output.

[a, b, c] [1, 2, 3, 4] [A.class::AA, A.class::BB]

Thats all for creating an Array of ArrayList and ArrayList of Array in Java.

You can checkout more core java examples from our GitHub Repository.

Reference: Java Arrays

Video liên quan

Chủ Đề