Array of ArrayList in Java example

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

714 total views, 1 views today

About Mirthbees 679 Articles
Blogger, Innovator, Developer and Enterpreneur. Founder of GuruRecharge, Readymade online recharge script. Lead Developer in Bookrate.in

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.