Results 1 to 8 of 8
Thread: Object Arrays
- 02-16-2012, 12:06 AM #1
Object Arrays
I need to make an array of multiple objects.
Like instead of:
Something like:Java Code:public MainBot mainBotObject1 = new MainBot(); public MainBot mainBotObject2 = new MainBot(); public MainBot mainBotObject3 = new MainBot();
Is this possible?Java Code:public MainBot mainBotObject[1] = new MainBot(); public MainBot mainBotObject[2] = new MainBot(); public MainBot mainBotObject[3] = new MainBot();
-
Re: Object Arrays
Yes, it's possible, give it a try and see.
- 02-16-2012, 12:32 AM #3
Re: Object Arrays
I looked it up and it says it needs to be declared like this...
Error says on the second line:Java Code:MainBot[] mainBotObject = new MainBot[2]; MainBot[0] = new MainBot();
']' expected
invalid method declaration; return type required
- 02-16-2012, 12:34 AM #4
Re: Object Arrays
First you need to declare the array of your class, with a type, name, and (optionally) initial value, just like any other variable:
When you create a new array of any object type, all its indexes are null. They do not automatically contain new objects. So you need to do something like this:Java Code:MainBot[] mainBots = new MainBot[3];
Java Code:for(int i = 0; i < mainBots.length; ++i) { mainBot[i] = new MainBot(); }Get in the habit of using standard Java naming conventions!
- 02-16-2012, 12:35 AM #5
Re: Object Arrays
Get in the habit of using standard Java naming conventions!
-
Re: Object Arrays
Close but you're trying to use the class name where you should be using the array variable. Try:
But most importantly read the tutorials on how to use arrays as this will all be explained well there.Java Code:MainBot[] mainBotObject = new MainBot[2]; // MainBot[0] = new MainBot(); mainBotObject[0] = new MainBot();
- 02-16-2012, 01:09 AM #7
Re: Object Arrays

This doesn't seem to be picking up the thread's(MainBot) location:
Error:Java Code:MainBot[] mainBotObject = new MainBot[2]; mainBotObject[0] = new MainBot();
It says it's looking in the MainFrame class when it needs to be directed to the MainBot class in order to find my thread.cannot find symbol
symbol: class mainBotObject
location: class MainFrame
']' expected
invalid method declaration; return type required
-
Re: Object Arrays
You'll need to show more code as I can't tell what you're doing wrong. For all I know, you could be trying to call this code out naked in the class itself and outside of a method or constructor.
Similar Threads
-
Help with object arrays
By kingkongjaffa in forum New To JavaReplies: 1Last Post: 08-04-2011, 04:30 PM -
Object Orientation - Arrays
By Boysie in forum Jobs DiscussionReplies: 4Last Post: 04-14-2010, 09:32 PM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Object Arrays
By rsvr in forum New To JavaReplies: 9Last Post: 12-05-2009, 11:27 PM -
Stuck with Java Object Orientated Programming - Arrays
By Phalanx in forum New To JavaReplies: 2Last Post: 12-10-2008, 04:40 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks