Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
create txt file in spe
populate dropdown in j
Replace
java excel
Player Class in the Ja
printing jdbc batch qu
add one tree object co
rmi jdbc connection
myfaces tomahawk
how to pass same varia
Passing Parameters in
covert uppercase to lo
Java Swing Center a Di
java script to get par
token string
Jini's relevance emerg
treemap with comparabl
java set methods
java registration form
How to write JSP
java application code
insert fields blob
pst.setInt()
jboss
buffer reader class no
jdbc interview questio
jsp:forward
Dynamic Instantiation
key send
Photoshop Abstracts An
KnowledgeTree
decimal to binary conv
Database MySQL Retriev
upload files using ser
store
cluster
dog tags
Eclipse 3.4 EJB
Photoshop Web Graphics
tomahawk menu
structs
mail merge in word
make runing programs
create table using jsp
java zip
java merging images
example of JFileChoos
Photoshop Animation Fa
square
iBatis packaged functi
interceptors hibernate
JMF
jsps in flex
validate javascript
EJB 2.0 tutorials
java voice chating
get file name
Servlet Container
text_io.file_type
Photoshop Abstracts Ra
code for simple calcul
write in a file valu
jsf datatable
struts hello
session bean and hiber
primitive data types u
java installtion
jsp forms getparameter
login=
Implementasi Stack wit
reading excel file usi
check a string for spa
develop graphs
Jintegra
JavaWorld article abou
java servlet
Struts Validator Frame
console program to wri
UI tag
how to parse
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.