Click to See Complete Forum and Search --> : SOLVED - Arrays in Java
Stubzz
Apr 9th, 2002, 06:00 PM
I am trying to set up an array that hold objects that i am creating, however their parameters are of different types and i get an 'incompatable types error when i try to compile.
This is the code that i am using:
private Shape[] shape = new Shape[6];
int [] object= new Shape [6];
shape[0] = new Circle(40, 40, 30, Color.red);
shape[1] = new House(80, 80, 30, Color.yellow);
shape[2] = new Square(120, 120, 30, Color.blue);
shape[3] = new Diamond(160, 160, 30, Color.green);
shape[4] = new Face(200, 200, 30, Color.pink);
shape[5] = new Face(240, 240, 30, Color.black);
Object has the following parameter types: Object(int x, int y, int s, string)
I'm new to java so its probbly a silly mistake. Can someone tell me what I'm doing wrong?
honeybee
Apr 10th, 2002, 04:18 AM
What classes are you using? I have not yet come across any classes called Objects. If you have a class myObject (I am not using the name Object, as it's actually a Java class, I think), and want to have an array of objects of the myObject class, your code would be:
private myObject[] objects = new myObject[6];
objects[0] = new myObject(40, 40, 30, Color.white);
objects[1] = new myObject(80, 80, 30, Color.yellow);
objects[2] = new myObject(120, 120, 30, Color.red);
objects[3] = new myObject(160, 160, 30, Color.black);
objects[4] = new myObject(200, 200, 30, Color.pink);
objects[5] = new myObject(240, 240, 30, Color.black);
.
Stubzz
Apr 10th, 2002, 06:01 AM
Im using custom sub-classes, i changed the classes to make it more generic. I've changed it now
honeybee
Apr 10th, 2002, 06:56 AM
Try taking out the second line then:
private Shape[] shape = new Shape[6];
//int [] object= new Shape [6];
shape[0] = new Circle(40, 40, 30, Color.red);
shape[1] = new House(80, 80, 30, Color.yellow);
shape[2] = new Square(120, 120, 30, Color.blue);
shape[3] = new Diamond(160, 160, 30, Color.green);
shape[4] = new Face(200, 200, 30, Color.pink);
shape[5] = new Face(240, 240, 30, Color.black);
The commented line would be a mistake, as you are creating an array named object of type integer, but assigning it a reference to a new Shape type array. Also commenting the second line should not affect your desired output, to the best of my knowledge.
.
Stubzz
Apr 10th, 2002, 07:04 AM
Taking out that line doesn't make a difference. The code is currently at the top of the page, but when i move it to the bottom, i get the error: 'can't resolve symbol':confused:
CaptainPinko
Apr 10th, 2002, 04:43 PM
are you sure that circle, house, face etc. are subclass of Shape? Ie. in their class they explicitly state extends Shape?
what symbol does it say that it can't resolve?
honeybee
Apr 11th, 2002, 10:10 AM
Originally posted by Stubzz
Taking out that line doesn't make a difference. The code is currently at the top of the page, but when i move it to the bottom, i get the error: 'can't resolve symbol':confused:
Can you post the entire code?
.
Stubzz
Apr 11th, 2002, 12:06 PM
problem solved now:D thanks for the help guys
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.