Unable to execute ArrayList member's method
Hi all,
I have some code where I store different objects, each of which will have it's own method to be executed when the object is retrieved.
However, the compiler does not recognize the class of the object, and thus the relevant method is not recognized.
Could someone please show how this can be properly done?
I enclose a simple code example.
import java.util.*;
publicclass ArrayListExample{
ArrayList a=new ArrayList();
publicstaticvoid main(String[] args){
ArrayListExample e=new ArrayListExample();
Apples ap=new Apples();
e.a.add(ap);
// getN() is not recognized by compiler at compile time.
System.out.println("There are "+e.a.get(0).getN()+" apples");
}
}
class Apples{
privateint n=10;
publicint getN(){return n;}
}
[1639 byte] By [
mathmatea] at [2007-11-26 14:08:13]

All the compiler knows is that it is an object. You need to cast it. Or if you are using 1.5+ use generics. Also you might want to research polymorphism.
ArrayList.get() returns a reference to Object. The compiler can't know that at runtime you'll actuall be putting an Apple in there. It says, "get returns Object, and Object doesn't have this method. This is an error."
Before 1.5, you cast the returned reference: Apple apple = (Apple)list.get(0);
apple.someAppleMethod();
Starting with 1.5, you can use generics (http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html, http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf):
List<Apple> list = new ArrayList<Apple>();
// ... add to list ...
Apple apple = list.get(0);
apple.appleMethod();
// OR, as you're trying to do...
list.get(0).appleMethod();
Thank you for the suggestions. I have gone through briefly the tutorial, which proves to be interesting if the ArrayList contains elements of a single known type.
My intention is to use the ArrayList to store elements of different classes, such as a list of fruits, say apples, oranges, all derived from Fruits. Each individual fruit would define the abstract method draw. So by using generics defining the list as <Fruits>, the compiler knows that the element is a fruit. If I execute the abstract method draw at execution time, I am hoping that the respective (orange, apple...) draw method will be used. For example:
List<Fruits> list = new ArrayList<Fruits>()
...
list.get(0).draw(); // could be apples or oranges
Does the reasoning sound OK to you?
> My intention is to use the ArrayList to store
> elements of different classes,
Generally a bad idea, unless those classes have a common parent class or implement a common interface...
> such as a list of
> fruits, say apples, oranges, all derived from Fruits.
...such as List<Fruit>. And then you only call methods that are defined in Fruit or above. If you're still calling methods that only Apple defines, you probably should be putting Apples and Bananas in the same list.
> Each individual fruit would define the abstract
> method draw. So by using generics defining the list
> as <Fruits>, the compiler knows that the element is
> a fruit.
Right.
> If I execute the abstract method draw at
> execution time, I am hoping that the respective
> (orange, apple...) draw method will be used. For
> example:
It will.
> code]
> List<Fruits> list = new ArrayList<Fruits>()
> ...
> list.get(0).draw(); // could be apples or
> oranges[/code]
>
> Does the reasoning sound OK to you?
Yup. That will work as long as the base class or interface Fruit defines that draw method. That's exactly the way to do it.
Thanks a million jverd. I'll now get the ball rolling!
It's also good to note that generic collections and the enhanced for loop play well together:
List<Fruit> salad = new ArrayList<Fruit>();
... toss in fruit ...
for (Fruit piece : salad) { // < Sweet like mango
piece.draw(); //etc
}
Reference: http://java.sun.com/j2se/1.5.0/docs/guide/language/foreach.html
Just want to let everyone know, I have recoded the way we discussed, and it works like a charm. Dr. Laszio, the V1.5 enhanced for-loop does lead to more readable code. Thanks to all!
Cool. I'm glad it you got it.