Java ArrayList

The java.util.ArrayList class is one of the most commonly used of all the classes in the Collections Framework.

ArrayList is Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.

Some Imortanat points about ArrayList:

  • It can grow dynamically.
  • It provides more powerful insertion and search mechanisms than arrays.
  • It Contains dulicate elements.
  • ArrayList Cares about Index.So ArrayList Provides Insertion order of elements.
  • It contains null values also.
  • It implements Serializable,clonable,Random access Interfaces.
  • It Provides Fast Iteration.
  • ArrayList is Non Synchronized.
  • ArrayList class Methods

    public boolean add(E o)

    This Method Will Appends the specified element to the end of this list.


    public void add(int index, E element)

    This Method Will Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

    in above line will add the "AA" element to the arraylist 2nd position.

    public E remove(int index)

    This Method will Removes the element at the specified position in this list.

    above line will removes the arraylist element at 3rd position.

    public boolean addAll(Collection c)

    This method will Appends all of the elements in the specified Collection to the end of this list, in the order that they are returned by the specified Collection's Iterator. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress.


    public void clear()

    This Method will Removes all of the elements from this list. The list will be empty after this call returns.


    public E get(int index)

    This Method Returns the element at the specified position in this list.

    above line returns the element at arraylist 2nd position.

    public E set(int index, E element)

    This Method Replaces the element at the specified position in this list with the specified element.


    above line will replace the arraylist 2nd position value with "bbb".

    public int size()

    This Method Returns the number of elements in this list.

    This Method returns the size of the arraylist.

    Constructors of ArrayList:

    ArrayList arraylist = new ArrayList();

    Constructs an empty list with an initial capacity of ten.

    ArrayList arraylist = new ArrayList(int initial capacity);

    Constructs an empty list with the specified initial capacity.

    ArrayList arraylist = new ArrayList(Collection c);

    Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

    till Now We Have Seen Details about ArrayList,Now We will See One Example On ArrayList.


    package com.javalchool.practice;

    import java.util.ArrayList;

    public class ArrayListDemo {

    public static void main(String[] args){

    //Creation Of ArrayList Constructor

    ArrayList arraylist = new ArrayList();

    //adding elements to the list
    //displaying the arraylist elements before adding duplicate elements

    System.out.println("ArrayList Elements " +arraylist);
    //adding duplicate element
    //displaying the arraylist elements after adding duplicate elements
    System.out.println("ArrayList after Adding The Duplicate elements"+arraylist);

    //adding the null values to the arraylist

    System.out.println("ArrayList After Adding Null"+arraylist);

    //Inserting element specified position in this arraylist
    arraylist.add(2, "GGG");

    //removing the 4th position element in arraylist

    System.out.println("arraylist after removing the 4th positon element"+arraylist);

    //adding all arraylist elements to the arraylist




    ArrayList Elements [AAA, BBB, CCC, DDD]
    ArrayList after Adding The Duplicate elements[AAA, BBB, CCC, DDD, AAA, BBB]
    ArrayList After Adding Null[AAA, BBB, CCC, DDD, AAA, BBB, null]
    [AAA, BBB, GGG, CCC, DDD, AAA, BBB, null]
    arraylist after removing the 4th positon element[AAA, BBB, GGG, CCC, AAA, BBB, null]
    [AAA, BBB, GGG, CCC, AAA, BBB, null, AAA, BBB, GGG, CCC, AAA, BBB, null]

    No comments:

    Post a Comment