Reputation: 89
I'm getting an error that states "cannot find method - getCenter" however whenever I replace the "objects.get(i)" with a single object like a, b, or d, it finds the method just fine. I don't understand why it can't find the method from the ArrayList.
public class TestPoly
{
public static void main (String []args)
{
ArrayList objects = new ArrayList();
Circle2 a = new Circle2(33,10,4);
Cylinder2 b = new Cylinder2(21,15,40,5);
Oval2 c = new Oval2(3,7,34,10);
OvalCylinder2 d = new OvalCylinder2(5,11,5,5,7);
objects.add(a);
objects.add(b);
objects.add(c);
objects.add(d);
for (int i = 0; i < objects.size(); i++)
{
System.out.println("For " + objects.get(i).getClass().getName() + objects.get(i).getCenter());
}
}
}
Circle2 is this.
public class Circle2
{
// instance variables
private int x;
private int y;
private int radius;
/**
* Constructor for objects of class circle
*/
public Circle2(int p_x, int p_y, int r)
{
// initialise instance variables
x = p_x;
y = p_y;
radius = r;
}
public int getRadius()
{
return radius;
}
public String getCenter()
{
return " the center is at (" + x + "," + y + ")";
}
}
Cylinder2, Oval2, and OvalCylinder2 are all just subclasses of Circle2.
Upvotes: 2
Views: 2181
Reputation: 29166
You are creating an ArrayList
without any type parameter, which means you are creating it as a Raw Type. When you do that, you will need to cast the objects to the appropriate type when you get them from the list.
To run your code appropriately in this case, which is the recommended way, specify a type parameter like this -
// if you are using jdk 7 or above
ArrayList<Circle2> objects = new ArrayList<>();
or -
ArrayList<Circle2> objects = new ArrayList<Circle2>();
See more about generics here.
However, if you still want to make your existing code work (not recommended), then use this -
ArrayList objects = new ArrayList();
Circle2 a = new Circle2(33,10,4);
Cylinder2 b = new Cylinder2(21,15,40,5);
Oval2 c = new Oval2(3,7,34,10);
OvalCylinder2 d = new OvalCylinder2(5,11,5,5,7);
objects.add(a);
objects.add(b);
objects.add(c);
objects.add(d);
for (int i = 0; i < objects.size(); i++)
{
Circle2 circle = (Circle2 )objects.get(i);
System.out.println("For " + circle.getClass().getName() + circle.getCenter());
}
Don't use raw types in this way. They are not meant to be used in the new code, they are supported just for backward compatibility. From the above link about raw type -
Raw types show up in legacy code because lots of API classes (such as the Collections classes) were not generic prior to JDK 5.0. When using raw types, you essentially get pre-generics behavior.
Upvotes: 3
Reputation: 21047
An ArrayList
(or any other collection) needs to know the class of object that it's going to store. If none is specified, then it assumes that you will store Object
instances.
To specify the class that your collection will store, you need to include the class name when you create your collection:
// ...
ArrayList<Circle2> objects = new ArrayList<Circle2>();
// ...
Now you can read over your ArrayList
two ways:
The 'array' style: You use an index to go through every entry in your ArrayList
:
for(i=0; i<objects.size(); i++) {
// Use objects(i) to get the Circle2 instance
}
The 'iterator' style: Objects stored in a collection can be read by iterating over the collection:
for(Circle2 c : objects) {
/*
* The object 'c' stores every single Circle2 instance in your 'objects' list
*/
}
I suggest you check the Collections
and the Generics
tutorials:
Upvotes: 0
Reputation: 1741
The current arraylist that you have right now ... takes "Object" as its elements ... and as a result ... its elements will only support the methods defined on an object (toString, getHashcode etc)
You can define the type like:
ArrayList<Circle2> objects = new ArrayList<Circle2>();
However then it can only accept objects of type circle. And following lines will cause an error:
objects.add(b);
objects.add(c);
objects.add(d);
A better way to do this is to use inheritence ... so you will have a base class called shape ... which will have a method called getCenter which will be overridden (if needed) by the subclasses
public abstract class Shape {
public String getCenter();
}
have four classes extend the shape class:
public class Cylinder extends Shape
public class Circle extends Shape
and so on
So now you can have your arraylist as:
ArrayList<Shape> shapes = new ArrayList<Shape>();
And add multiple different shapes in it:
shapes.add(new Circle())
shapes.add(new Cylinder())
...
And the following code is possible
for(Shape shape : shapes)
System.out.println(shape.getCenter())
Upvotes: 1
Reputation: 279920
You haven't declared a Type parameter or Type argument for your ArrayList
, so it defaults to Object
. Object
doesn't have a getCenter()
method.
You can change your ArrayList
declaration to something like:
ArrayList<Circle2> objects = new ArrayList<Circle2>();
Circle2 a = new Circle2(33,10,4);
objects.add(a);
objects.get(0).getCenter();
but it can now only take Circle2
objects.
Upvotes: 5