• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ArrayList

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic