Array of ArrayList in Java example

(Last Updated On: August 11, 2016)

Array of ArrayList:
Array of ArrayList has to be typecasted first.
ArrayList array has to be initialized first with ArrayList.
Then we have to add the values to the ArrayList.
Iterator has to be created separately for each arraylist of array.

Program:

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayofArrayList {

	public static void main(String[] args) {
		ArrayList[] myArrayArrayList = (ArrayList[])new ArrayList[10]; // 10 is the size of the array not arraylist
		myArrayArrayList[4] = new ArrayList(); //The array has to be initialized with arraylist
		myArrayArrayList[4].add("Nokia");
		myArrayArrayList[4].add("Samsung");
		myArrayArrayList[4].add("Sony");
		myArrayArrayList[4].add("Lg");
		myArrayArrayList[4].add("Micromax");
		myArrayArrayList[4].add("Ericson");
		myArrayArrayList[4].add("Motorola");
		myArrayArrayList[4].add("G5");
		myArrayArrayList[4].add("G7");
		myArrayArrayList[4].add("Karbon");
		myArrayArrayList[4].add("Reliance");
		Iterator iterArrayArrayList = myArrayArrayList[4].iterator();
		while(iterArrayArrayList.hasNext()){
			System.out.println(iterArrayArrayList.next());
		}

	}

}

Output:
Nokia
Samsung
Sony
Lg
Micromax
Ericson
Motorola
G5
G7
Karbon
Reliance

154 total views, 1 views today

Leave a Reply

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