How to call add methods for ArrayList objects

L

lonelyplanet999

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++) {

}

}
}
 
A

Anthony Borla

lonelyplanet999 said:
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
 
T

Tim Blanchard

lonelyplanet999 said:
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())'
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top