• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList

 
Venkat Ramsimha
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider the following code:
import java.util.*;
class ArrayDemonstration
{
public static void main(String args[])
{
ArrayList list = new ArrayList(2);//line 1
list.add("mango");
list.add("apple");
list.add("mango");
list.add("banana");
Iterator i = list.iterator();
while(i.hasNext())//line 2
{
System.out.print(i.next());//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,
carl..
 
Animesh Shrivastava
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello carl,
Go through the java api
http://java.sun.com/j2se/1.4.2/docs/api/index.html,

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic