|
-
Apr 9th, 2002, 05:00 PM
#1
SOLVED - Arrays in Java
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:
PHP Code:
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?
-
Apr 10th, 2002, 03:18 AM
#2
Well ...
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:
Code:
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);
.
-
Apr 10th, 2002, 05:01 AM
#3
Im using custom sub-classes, i changed the classes to make it more generic. I've changed it now
-
Apr 10th, 2002, 05:56 AM
#4
Well ...
Try taking out the second line then:
Code:
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.
.
-
Apr 10th, 2002, 06:04 AM
#5
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'
-
Apr 10th, 2002, 03:43 PM
#6
Hyperactive Member
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?
"There are only two things that are infinite. The universe and human stupidity... and the universe I'm not sure about." - Einstein
If you are programming in Java use www.NetBeans.org
-
Apr 11th, 2002, 09:10 AM
#7
Well ...
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'
Can you post the entire code?
.
-
Apr 11th, 2002, 11:06 AM
#8
problem solved now thanks for the help guys
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|