3

My previous OOP experience has been with Objective-C (which is dynamically typed), however, I am now learning Java. I want to iterate over an ArrayList of objects and perform a certain method on them. Every object in the ArrayList is of the same class. In Objective-C, I would just check in each iteration that the object was the correct class, and then run the method, but that technique is not possible in Java:

for (Object apple : apples) {
        if (apple.getClass() == Apple.class) {
            apple.doSomething(); //Generates error: cannot find symbol
        }
    }

How do I 'tell' the compiler which class the objects in the ArrayList belong to?

2
  • Is this an list of only apples (as the name "apples" suggests) or is it a heterogenous list that contains apples and some other stuff? danben's suggestion is the right way to go for the former. akf's answer is a more literal translation of what you posted, but really only makes sense for a heterogenous list (ie: where you expected non-apples to be in the list). Commented Feb 4, 2010 at 0:32
  • I'll edit to make it more clear. Commented Feb 4, 2010 at 0:34

4 Answers 4

10

In Java 5 and later, collecton types are generified. So you would have this:

ArrayList<Apple> a = getAppleList(); // list initializer

for (Apple apple : a) {
    apple.doSomething();
}

It is not generally good practice to have ArrayLists of Object unless you specifically need your ArrayList to be able to hold different types of Objects. Usually that is not the case, and you can use heterogenous collections for increased type-safety.

Sign up to request clarification or add additional context in comments.

2 Comments

Might be a bit clearer if you just said "Apple"/"apple" instead of "MyClass"/"myclass".
I changed that quite a while ago; maybe you hadn't refreshed the page?
5

for traditional casting, consider this:

for (Object apple : apples) {
    if (apple instanceof Apple) { //performs the test you are approximating
        ((Apple)apple).doSomething(); //does the cast
    }
}

in later versions of Java, Generics were introduced that obviate the need for these sorts of tests.

1 Comment

That's a new-style for loop, so I'm pretty sure he's got generics. Also, even pre-generics you'd generally forgo the instanceof check unless you were actually expecting to skip over non-Apples (and if the latter is true, generics don't help you anyway).
1

Reading the section on casting from the Java Tutorial should answer that question.

(Or, if you declare the ArrayList yourself, use an approapriate type parameter as danben suggests=

2 Comments

It didn't, and anyway, you're not meant to tell people to RTFM on SO.
You asked: "How do I tell the compiler which class the objects belong to". The tutorial writes: "However, we can tell the compiler that we promise to assign a MountainBike to obj by explicit casting: MountainBike myBike = (MountainBike)obj;"
0

You need to cast Object apple to Apple.

((Apple)apple).doSomething();

But in this specific case, it's better to use;

for(Apple apple : apples){
    apple.doSomething();
}

2 Comments

Actually if his collection is not properly typed (with generics), this won't work. Which SUCKS by the way.
I just trusted his asumptation, "Every object in the ArrayList is of the same class."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.