Home Java Example Java Core Arraylist in java
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Arraylist in java

Advertisement
ArrayList is a class that extends AbstractList and implements List Interface. Standard java array are of fixed length, that mean we should know the size of array how many element it will hold, they cannot grow or shrink. but for indefinite number of elements

ArrayList is a class that extends AbstractList and implements List Interface. Standard java array are of fixed length, that mean we should know the size of array how many element it will hold, they cannot grow or shrink. but for indefinite number of elements Arraylist is used as it creates dynamic Array. Arraylist allowed to store an ordered group of elements where duplicates are allowed. Arraylist are created with an initial size, if the size  exceeds, the collection enlarges automatically. When objects are removed the shrinks automatically.

ArrayList has three constructor shown here.

  • ArrayList( ) : This will create a empty ArrayList.  
  • ArrayList(Collection c ) : containing the elements of the specified collection, in the order they are returned by the collection's iterator. c whose element is to be placed into this list. It will throw NullPointerException if the specified collection is null. 
  • ArrayList(int initial Capacity ) : This will construct an empty array list specified with initial capacity. It will throw a IllegalArgumentException if the capacity is negative.

Some of the methods of ArrayList as follows.

Method Summary
boolean add(E e) - Add the specified element into the list.
void add(int index, e Element) - Insert the element at the specified position in the list
boolean addAll(Collection c) -  Add all the element in to the end of the list in the order of collection.
boolean add(int index, Collection c) - Inserts all of the element to the list ,index is starting position in the list.
void clear() - Remove all the content from he list.
E remove(int index) Remove the element at the specified position in the list.
boolean remove(Object o) - Remove the first occurrence of the element ,if it present.
Object clone() - Return the shallow copy of ArrayList instance.

A simple program of Arraylist.

import  java.util.*;
public class ListDemo
  {
    public static void main(String args[])
    {
        ArrayList al=new ArrayList();
        al.add("A");
        al.add("B");
        al.add("C");
        al.add("D");
        al.add("E");
        System.out.println("The ArrayList is "+al);
        
        //removing element C from the list. 
        al.remove("C");    
        System.out.println("The ArrayList is "+al);
        
       //removing element at the second position that is B.
        al.remove(1);      
       System.out.println("The ArrayList is "+al);
       
       //adding element G at the  first  position.
        al.add(0,"G");     
       System.out.println("The ArrayList is "+al);
       }
    }
Output: After compiling and executing  the above program output will be as follows.

Download Source Code
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 6, 2013

Related Tutorials

Ask Questions?    Discuss: Arraylist in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com