Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to call add methods for ArrayList objects

Reply
Thread Tools

How to call add methods for ArrayList objects

 
 
lonelyplanet999
Guest
Posts: n/a
 
      11-11-2003
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++) {

}

}
}
 
Reply With Quote
 
 
 
 
Anthony Borla
Guest
Posts: n/a
 
      11-11-2003

"lonelyplanet999" <> wrote in message
news: om...
> 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


 
Reply With Quote
 
 
 
 
Tim Blanchard
Guest
Posts: n/a
 
      11-11-2003

"lonelyplanet999" <> wrote in message
news: om...
> 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())'


 
Reply With Quote
 
lonelyplanet999
Guest
Posts: n/a
 
      11-11-2003
"Anthony Borla" <> wrote in message news:<jhZrb.6155$>...
> "lonelyplanet999" <> wrote in message
> news: om...
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Clear Arraylist vs new ArrayList Philipp Java 6 05-28-2008 09:07 PM
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments