Hi
You have a number of choices (as usual...).

1. use the 'enhanced' form of for-loop:

        for (Object o: collection) {
                System.out.println(o);
        }

2. or you could use an iterator:

        Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
                Object o = iterator.next();
                System.out.println(o);
        }

You can use the Object type like these examples which should work for  
pretty much anything, or if you have a parent class such as Drink, you  
could use that as the reference type for 'o' instead of Object. If you  
need to check what type of Object or Drink you have with each element  
in the ArrayList then you can use the instanceof keyword:

        if ( o instance of Juice) {
                //do something juicy here
        }

The main reason to use an Iterator is if you want the program to  
remove elements from the collection, or to traverse more than one  
collection in the same loop.

Hope this helps.

On 31/12/2009, at 10:36 AM, DrybLrac wrote:

> hello all I have an arraylist problem...I created a program to store
> coffee and juice objects in a arraylist bout how do I display them
> with a for loop. Inside the for loop I know I need  a if statement to
> see if the current object in the for loop is coffee or juice. but
> don't know how to write it...any suggestions?
>
> -- 
> To post to this group, send email to 
> [email protected]
> To unsubscribe from this group, send email to 
> [email protected]
> For more options, visit this group at 
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to