ArrayList Class In Java

 

First implementation class of List interface is ArrayList.It is very easy to and useful. ArrayList provides index basis operation. Java ArrayList Class uses a dynamic array to store the elements. Java ArrayList Class acquires AbstractList class and implements List interface.

The important points about Java ArrayList class are:

  1. Java ArrayList class can contain/store duplicate elements. In ArrayList store duplicate value.
  2. Java ArrayList class maintains insertion order, means which order you store the value return is also same.
  3. Java ArrayList class is none synchronized. Multiple threads not working in same time.
  4. Java ArrayList allows random access. You can iterate the ArrayList elements index basis because array works at the index basis.ArrayList internally use the dynamic array.
  5. In Java ArrayList class, manipulation is slow because a lot of shifting needs to occur if any element is removed from the array list.

 

Methods of Java ArrayList

Method Description
                                                             void add(int index,Object element)                   Insert element specified position.
boolean addAll(Collection c) This method used to append all of the elements in the stipulated collection to the end of this list, in the order that they are returned by the specified collection's iterator.

void clear()

It is used to eliminate all of the elements from this list.
int lastIndexOf(Object o) It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
Object[] toArray() It is used to return an array containing all of the elements in this list in the correct order.
Object[] toArray(Object[] a) It is used to return an array containing all of the elements in this list in the correct order.

boolean add(Object o)

It is used to append the specified element to the end of a list.

boolean addAll(int index, Collection c)
 
It is used to insert all of the elements in the specified collection into this list, starting at the specified position.

Object clone()
 
It is used to return a shallow copy of an ArrayList.

int indexOf(Object o)
 
It is used to return the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.

void trimToSize()
 
It is used to trim the capacity of this ArrayList instance to be the list's current size.

 

Example :-

 

package CollectionOperaton;

import java.util.ArrayList;
import java.util.List;

public class ArrayListOperation {
    public static void main(String args[]){
        //First create a ArrayList String type
        List<String> arraylist = new ArrayList<String>();
        //Add the elements in arrayList
        arraylist.add("Tom");
        arraylist.add("Jeery");
        arraylist.add("Ducktales");
        
        for(String str:arraylist){
            System.out.println(str);
        }              
        }

}

 

Related Articles

post a comment