ArrayList

Keep in mind
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
Example 1 : Iterating through while loop
package com.tutorial.one;

import java.util.*;
public class CollectionArrayList {
 public static void main(String args[]){
 ArrayList<String> list = new ArrayList<String>();
 list.add("Tarun");
 list.add("Nakul");
 list.add("Prathith");
 list.add("Parth");
 
 Iterator itr = list.iterator();
 while(itr.hasNext()){
 System.out.println(itr.next());
 }
 }
}
Example 2 : Iterating through for-each loop
package collection;

import java.util.*;
public class ArrayListExample {
 public static void main(String args[]){
 ArrayList<String> list = new ArrayList<String>();
 list.add("Tarun");
 list.add("Valera");
 list.add("Khatri");
 for(String obj:list)
 System.out.println(obj);
 }
}

 

 

Leave a Reply

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

Proudly powered by WordPress | Theme: Baskerville 2 by Anders Noren.

Up ↑