This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
How do I create an array of objects? For example I have a class that makes a rectangle. I am using another applet class to draw the rectanfgle to the screen. I'm trying to create an array of rectangle objects to make multiple rectangles on the screen. Thanks for your help.
The following would create an array, called polygons, of six references to objects of type Rectangle... //create references to Rectangle objects Rectangle rec1, rec2, rec3, ... // create your array, called polygons Rectangle polygons = new Rectangle; // now assign rectangle objects to // each array element... polygons = new rec1; polygons = new rec2; ...
[This message has been edited by Allen Alchian (edited November 25, 2000).] [This message has been edited by Allen Alchian (edited November 25, 2000).]
Joined: Nov 24, 2000
Thanks for your help. When I try to do this, I keep getting a syntax error saying that another  is expected in the line: polygons = new rect1;
Laura, try this // initialize the rectangle objects after creating array and // rectangle objects as mentioned by Allen rec1 = new Rectangle ( 20,30,40,50); rec2 = new Rectangle ( 40,40,40,50); ..... // assign the rectangle objects to the array elements. // here the new is not required. polygons = rec1; polygons = rec2; Hope this helps (and compiles properly!). Kalidas.