How to call add methods for ArrayList objects

Discussion in 'Java' started by lonelyplanet999, Nov 11, 2003.

  1. Hi,

    I wrote below code which failed in compilation as compiler complained
    the methods add (java.lang.String) are can't solve symobols. The same
    method succeeded for LinkedHashSet & TreeSet objects. Why it failed
    for ArrayList objects ? The syntax description for ArrayList add
    method accepts object type instance.

    import java.util.*;

    public class ArrayListx {
    public static void main (String [] args) {

    ArrayList arl = new ArrayList();
    arl.add("Chicago");
    arl.add("Detroit");
    arl.add("Atlanta");
    arl.add("Denver");

    Iterator it = arl.iterator();
    while (arl.hasNext()) {
    System.out.println("city "+it.next());
    }

    for (int i=0; i<4; i++) {

    }

    }
    }
     
    lonelyplanet999, Nov 11, 2003
    #1
    1. Advertising

  2. "lonelyplanet999" <> wrote in message
    news:...
    > Hi,
    >
    > I wrote below code which failed in compilation as compiler complained
    > the methods add (java.lang.String) are can't solve symobols. The same
    > method succeeded for LinkedHashSet & TreeSet objects. Why it failed
    > for ArrayList objects ? The syntax description for ArrayList add
    > method accepts object type instance.
    >

    <SNIP>

    Try:

    ...
    while (it.hasNext()) {
    ...

    replacing:

    while (arl.hasNext()) {

    I hope this helps.

    Anhony Borla
     
    Anthony Borla, Nov 11, 2003
    #2
    1. Advertising

  3. "lonelyplanet999" <> wrote in message
    news:...
    > Hi,
    >
    > I wrote below code which failed in compilation as compiler complained
    > the methods add (java.lang.String) are can't solve symobols. The same
    > method succeeded for LinkedHashSet & TreeSet objects. Why it failed
    > for ArrayList objects ? The syntax description for ArrayList add
    > method accepts object type instance.
    >
    > import java.util.*;
    >
    > public class ArrayListx {
    > public static void main (String [] args) {
    >
    > ArrayList arl = new ArrayList();
    > arl.add("Chicago");
    > arl.add("Detroit");
    > arl.add("Atlanta");
    > arl.add("Denver");
    >
    > Iterator it = arl.iterator();
    > while (arl.hasNext()) {

    ^^^

    > System.out.println("city "+it.next());
    > }
    >
    > for (int i=0; i<4; i++) {
    >
    > }
    >
    > }
    > }


    you want 'while (it.hasNext())'
     
    Tim Blanchard, Nov 11, 2003
    #3
  4. "Anthony Borla" <> wrote in message news:<jhZrb.6155$>...
    > "lonelyplanet999" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I wrote below code which failed in compilation as compiler complained
    > > the methods add (java.lang.String) are can't solve symobols. The same
    > > method succeeded for LinkedHashSet & TreeSet objects. Why it failed
    > > for ArrayList objects ? The syntax description for ArrayList add
    > > method accepts object type instance.
    > >

    > <SNIP>
    >
    > Try:
    >
    > ...
    > while (it.hasNext()) {
    > ...
    >
    > replacing:
    >
    > while (arl.hasNext()) {
    >
    > I hope this helps.


    Tks :)

    >
    > Anhony Borla
     
    lonelyplanet999, Nov 11, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Saravanan Rathinavelu

    Iterate through ArrayList using an another ArrayList

    Saravanan Rathinavelu, Aug 16, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    2,779
    Natty Gur
    Aug 19, 2003
  2. Kaidi
    Replies:
    4
    Views:
    2,456
    Kaidi
    Jan 3, 2004
  3. xz
    Replies:
    16
    Views:
    2,423
  4. Philipp
    Replies:
    6
    Views:
    950
    Arne Vajhøj
    May 28, 2008
  5. Kenneth McDonald
    Replies:
    5
    Views:
    352
    Kenneth McDonald
    Sep 26, 2008
Loading...

Share This Page