Tags

 

— ArrayList is a member of Collections Framework (which provides interfaces, implementation classes and algorithms to manipulate collections in a standard way) .

—  Unlike arrays, arraylists are dynamic in nature and their size does not need to be defined initially.

— Arraylists can grow or shrink after creation. You can specify capacity for ArrayList in constructor, which is completely optional. ArrayList can still shrink or grow.

— ArrayList class extends AbstractList class and implements the List interface.

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
public class TestArrayList {

 public static void main(String[] args){
 // Creating an Array List
 ArrayList myList = new ArrayList(2); //Entering the size is optional

 // Adding items to the Array List
 myList.add("BMW");
 myList.add("Hyundai");
 myList.add("Toyota");
 myList.add("Nissan");
 myList.add("Mazda");
 myList.add(123); // ArrayList has elements of String and int type

 // Size of the Array List
 System.out.println("Size of the ArrayList is: "+ myList.size());

 // Printing an ArrayList element by it's index and get method
 System.out.println(myList.get(2)); //Index starts at 0

 // Removing an element from ArrayList
 myList.remove("BMW"); //by value
 System.out.println("Contents of myList : "+ myList);

// Printing the index of an arraylist element
 int r = myList.indexOf("BMW");
 System.out.println(" Index of BMW is :" + r); //output = -1
 int t = myList.indexOf("Mazda");
 System.out.println(" Index of Mazda is :" + t); //output = 3

 // Updating an element of the ArrayList
 myList.set(3,"Mercedes");
 System.out.println("Contents of the array list: " + myList);
 myList.remove(3); // by Index
 System.out.println("Contents of myList : "+ myList);

// Accessing ArrayList elements: Method 1
 // Printing contents of entire ArrayLists
 System.out.println("Contents of myList : "+ myList);

 // Accessing ArrayList elements: Method 2
 // Looping with index
 System.out.println("Looping with Index");
 for(int i=0; i<myList.size();i++){
 System.out.println(myList.get(i));
 }

 // Accessing ArrayList elements: Method 3
 // Using Iterator
 System.out.println("Using Iterator and while loop");
 Iterator itr = myList.iterator();
 while(itr.hasNext()){
 System.out.println(itr.next());
 }

 //Accessing ArrayList elements: Method 4
 // Using Iterator and for loop
 System.out.println("Using Iterator and for loop");

 for(Iterator itrf = myList.iterator(); itrf.hasNext();){
 System.out.println(itrf.next());
 }

 // Removing all the elements of the ArrayList
 myList.clear();
 System.out.println("Contents of myList : "+ myList);// Initializing an ArrayList of specific data-types

 ArrayList<String> myList1 = new ArrayList<String>();
 myList1.add("Ala Romeo");
 myList1.add("Audi");
 myList1.add("Bentley");
 myList1.add("Bugatti");
 myList1.add("Chevrolet");

 //Finding whether an element is present in the ArrayList
 for (String s: myList1) {
 if(s.equals("Audi")) {
 System.out.println("Audi found!!");
 }
 }

 // Finding whether an ArrayList is empty
 boolean empty = myList1.isEmpty();
 System.out.println(empty);
}
}

 

Advertisements