ArrayList in Java | ArrayList Example with Program in Java - CodeTextPro

  Java ArrayList Examples

ArrayList is a part of collection framework and is present in java.util package. It provides us dynamic arrays in Java. Though, it may be slower than standard arrays but can be helpful in programs where lots of manipulation in the array is needed.

  1. ArrayList inherits AbstractList class and implements List interface.
  2. ArrayList is initialized by a size, however the size can increase if collection grows or shrunk if objects are removed from the collection.
  3. Java ArrayList allows us to randomly access the list.
  4. ArrayList can not be used for primitive types, like int, char, etc. We need a wrapper class for such cases.
  5. ArrayList in Java can be seen as similar to vector in C++.

Java Array list

import java.util.ArrayList;
public class MyBasicArrayList {
public static void main(String[] a){

ArrayList<String> al = new ArrayList<String>();
//add elements to the ArrayList
//get elements by index
System.out.println("Element at index 1: "+al.get(1));
System.out.println("Does list contains JAVA? "+al.contains("JAVA"));
//add elements at a specific index
System.out.println("Is arraylist empty? "+al.isEmpty());
System.out.println("Index of PERL is "+al.indexOf("PERL"));
System.out.println("Size of the arraylist is: "+al.size());

How to read all elements in ArrayList by using iterator?

import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListIterator {
public static void main(String a[]){
ArrayList<String> arrl = new ArrayList<String>();
//adding elements to the end
Iterator<String> itr = arrl.iterator();

Vector in Java
Java.util.Vector Class in Java. The Vector class implements a growable array of objects. Vectors basically falls in legacy classes but now it is fully compatible with collections. They are very similar to ArrayList but Vector is synchronized and have some legacy method which collection framework does not contain.

The Vector class implements a growable array of objects. Vectors basically falls in legacy classes but now it is fully compatible with collections.

  1. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index.
  2. They are very similar to ArrayList but Vector is synchronised and have some legacy method which collection framework does not contain.
  3. It extends AbstractList and implements List interfaces.

import java.util.*;
public class VectorDemo {
public static void main(String args[]) {

v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
System.out.println("Capacity after four additions: " + v.capacity());

v.addElement(new Double(5.45));
System.out.println("Current capacity: " + v.capacity());

v.addElement(new Double(6.08));
v.addElement(new Integer(7));
System.out.println("Current capacity: " + v.capacity());

v.addElement(new Float(9.4));
v.addElement(new Integer(10));
System.out.println("Current capacity: " + v.capacity());

v.addElement(new Integer(11));
v.addElement(new Integer(12));
System.out.println("First element: " + (Integer)v.firstElement());
System.out.println("Last element: " + (Integer)v.lastElement());

if(v.contains(new Integer(3)))
System.out.println("Vector contains 3.");

// enumerate the elements in the vector.
Enumeration vEnum = v.elements();
System.out.println("\nElements in vector:");

System.out.print(vEnum.nextElement() + " ");

Post a Comment