File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "ArrayList" Watch "ArrayList" New topic


Venkat Ramsimha
Ranch Hand

Joined: Dec 28, 2004
Posts: 127
consider the following code:
import java.util.*;
class ArrayDemonstration
public static void main(String args[])
ArrayList list = new ArrayList(2);//line 1
Iterator i = list.iterator();
while(i.hasNext())//line 2
System.out.print(;//line 3



On the above program i have some queries:
1.In the line marked with line1 the 'ArrayList ' has to accept only 2 elements but its accepting more than 2 elements .I would like to know whether its advisable to place the total no of elements in the 'ArrayList'
2.I would like to know the functionality of the lines marked with line2,line3

tx in advance,
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Hello carl,
Go through the java api,

it will give u a better explanation about that,
basically the constructor for the array list which u have used here,
specifies the capacity of the arraylist and not the size, thats not fixed and it increases whenever the number of elements goes beyond the capacity.
I agree. Here's the link:
subject: ArrayList
It's not a secret anymore!