Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Array List Example in java

 
 

Share on Google+Share on Google+

Array List Example in java

Advertisement
In this example we are going to show the use of java.util.ArrayList.

Array List Example in java

     

In this example we are going to show the use of java.util.ArrayList. We will be creatiing an object of ArrayList class and performs various operations like adding removing the objects.

Arraylist provides methods to manipulate the size of the array that is used internally to store the list. ArrayList extends AbstractList and implements List, Cloneable, Serializable ArrayList capacity .grows automatically. The ArrayList is not synchronized. It permits all elements including null.

In this program we are inserting a value. We are using three methods of ArrayList class.

add(Object o): Appends the specified element to the end of this list. It returns a boolean value.

size():  Returns the number of elements in this list.

remove(int index): Removes the element at the specified position in this list. It returns the element that was removed from the list. It throws IndexOutOfBoundsException : if index is out of range.

Code of a program is given below:

import java.util.*;

public class ArrayListDemo{
  public static void main(String[] args) {
  ArrayList<Object> arl=new ArrayList<Object>();
  Integer i1=new Integer(10);
  Integer i2=new Integer(20);
  Integer i3=new Integer(30);
  Integer i4=new Integer(40);
  String s1="tapan";
  System.out.println("The content of arraylist is: " + arl);
  System.out.println("The size of an arraylist is: " + arl.size());
  arl.add(i1);
  arl.add(i2);
  arl.add(s1);
  System.out.println("The content of arraylist is: " + arl);
  System.out.println("The size of an arraylist is: " + arl.size());
  arl.add(i1);
  arl.add(i2);
  arl.add(i3);
  arl.add(i4);
  Integer i5=new Integer(50);
  arl.add(i5);
  System.out.println("The content of arraylist is: " + arl);
  System.out.println("The size of an arraylist is: " + arl.size());
  arl.remove(3);
  Object a=arl.clone();
  System.out.println("The clone is: " + a)
  System.out.println("The content of arraylist is: " + arl);
  System.out.println("The size of an arraylist is: " + arl.size());
  }
}

The output of program will be like this:

C:\Java Tutorial>javac ArrayListDemo.java

C:\Java Tutorial>java ArrayListDemo
The content of arraylist is: []
The size of an arraylist is: 0
The content of arraylist is: [10, 20, tapan]
The size of an arraylist is: 3
The content of arraylist is: [10, 20, tapan, 10, 20, 30, 40, 50]
The size of an arraylist is: 8
The clone is: [10, 20, tapan, 20, 30, 40, 50]
The content of arraylist is: [10, 20, tapan, 20, 30, 40, 50]
The size of an arraylist is: 7

C:\Java Tutorial>

Download this example

Advertisement

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: June 7, 2007

Related Tutorials

Discuss: Array List Example in java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:6
shavn
April 22, 2011
hi

please send me a project about Queue in java programming.............Than you
dj
July 5, 2011
Arraylist

Thanks for this lecture!
D
April 15, 2012
Ads

Useful information, but you ads sucks!
Deepak yadav
September 12, 2012
core java

core java use in applet
Deepak yadav
September 12, 2012
core java

core java is the sensective language?
mallikarjun
November 17, 2011
arraylist

i want arraylist example
DMCA.com