OCJP

ArrayList in Java Collection

ArrayList is a Collection Class that is mostly used for Dynamic Array. ArrayList class in Java is defined in java.util pacakage (Utility Package). ArrayList can contain any type of elements. Default it contains the elements with type ‘Object’. As ArrayList implements List Interface, it has all commonly used List Functionalities. ArrayList is subclass of AbstractList class.

import java.io.*; 
import java.util.*; 

class ArrayListDemo1 
{ 
	public static void main(String[] args) 
	{ 
	ArrayList<String> countries = new ArrayList<String>(); //ArrayList Declared 
	
	countries.add("USA");
	countries.add("India");
	countries.add("UK");
	countries.add("Japan");
	
	System.out.println(countries);
	
	//Iterating all countries
	
	for(int i=0;i<countries.size();i++)
	{
	System.out.println(countries.get(i));
	}
	
	//Using For Each Loop
	System.out.println("Using For Each Loop ");
	for(String c : countries){
	System.out.println(c);
	}
	

	//Remove by Index
	
	countries.remove(2);//UK will be removed
	System.out.println(countries);
	
	} 
} 
ArrayList in Java

Leave a Reply

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


× How can I help you?