java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Use of ArrayList Class E-mail
User Rating: / 535
PoorBest 

The ArrayList class is a concrete implementation of List interface. This class supports dynamic arrays that can grow as needed. In java, standard arrays are of fixed length. After arrays are created, they cannot grow or shrink, which means you must know in advance how many elements an array will hold. However, sometimes you may not know how large an array you need. To handle this situation, collection framework has defined ArrayList class. An object of this can dynamically increase or decrease in size.

import java.util.ArrayList;

public class AraryListDemo {

  public static void main(String[] args) {

    ArrayList al = new ArrayList();
    System.out.print("Initial size of al :  " + al.size());
    System.out.print("\n");

    //add.elements to the array list
    al.add("C");
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");
    al.add(1,"A2");//inserts objects "A2" into array at index 1

    System.out.print("size of al after additions " + al.size());
    System.out.print("\n");

    //display the array list
    System.out.print("contents of al: " +  al );
    System.out.print("\n");

    //Remove elements from the array list
    al.remove("F");
    al.remove(2);

    System.out.print("size of after deletions : " + al.size());
    System.out.print("\n");
    System.out.print("contents of al:" + al);
    
  }

}

Output Screen:

Initial size of al:  0
size of al after additions 7
contents of al: [C, A2, A, E, B, D, F]
size of after deletions : 5
contents of al:[C, A2, E, B, D]

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.