Java ArrayList Example Code for Using ArrayList in Java
Content about : Java ArrayList
Advertisements
This Source code example for Create ArrayList Object and how to manage data in ArrayList and how to get Data from array List
Example code for Java ArrayList
package example.util;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample {
public void arrayListExample(){
//Create ArrayList Object
ArrayList<String> arrayList=new ArrayList<String>();
//Store and Remove data from ArrayList
arrayList.add(”value1″);
arrayList.add(”value2″);
arrayList.add(”value3″);
String value3=arrayList.remove(2);
System.out.println(”– Example for Get Value from ArrayList –”);
System.out.println(”Get ArrayList value: “+arrayList.get(0));
System.out.println(”Remove ArrayList value: “+value3);
System.out.println(”Contains ArrayList: “+arrayList.contains(”value1″));
System.out.println(”Contains ArrayList: “+arrayList.contains(”value3″));
System.out.println(”– Example for Iterate list from ArrayList –”);
Iterator<String> it=arrayList.iterator();
while(it.hasNext()){
String value=it.next();
System.out.println(”List Iterated Value: “+value);
}
System.out.println(”– Example for Loop from ArrayList –”);
for (String temp:arrayList){
System.out.println(”For value ArrayList “+temp);
}
}
public static void main (String args[]){
ArrayListExample arryList=new ArrayListExample();
arryList.arrayListExample();
}
}
Running Result
– Example for Get Value from ArrayList –
Get ArrayList value: value1
Remove ArrayList value: value3
Contains ArrayList: true
Contains ArrayList: false
– Example for Iterate list from ArrayList –
List Iterated Value: value1
List Iterated Value: value2
– Example for Loop from ArrayList –
For value ArrayList value1
For value ArrayList value2
Note : index of arrayList start from 0