Results 1 to 9 of 9
Thread: Array of references?
- 04-06-2011, 05:34 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Array of references?
I have 4 object arrays that hold different object types. I want to make an array to hold the references to each of these object arrays. What would be the type of this array? In C I would make an array of pointers but since pointers don't exist in java i don't know what to do.
Last edited by w00tguy123; 04-06-2011 at 06:05 AM.
- 04-06-2011, 05:44 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
While I could be wrong, and if I am I will learn something. I am fairly confident that an array of objects is an array of references.
- 04-06-2011, 05:48 AM #3
I have to ask why? This sounds like a bad idea.
- 04-06-2011, 06:02 AM #4
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Here's an example of what I'm trying to do:
Eventually, there will be about 20 different types in this array. Not having something like this makes it so I have to copy-paste blocks of codes, only changing the array being used in the block. This makes my code long and repetitive.Java Code:Bullet[] bullets = new Bullet[100]; RPG[] rpgs = new RPG[100]; Grenade[] grenades = new Grenade[100]; Shrapnel[] shrapnel = new Shrapnel[1000]; //something[] particleType = new something[4]; //particleType[0] = bullets; //particleType[1] = rpgs; //particleType[2] = grenades; //particleType[3] = shrapnel;
- 04-06-2011, 06:14 AM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
It sounds like you are dealing with an arsenal containing a whole lot of (arrays of) things.
You could consider making an Arsenal class. It would have the bullets, rpgs etc as part of its state and may very well have methods to do the things you are intending doing with the bullets etc in these "blocks of code".
- 04-06-2011, 06:20 AM #6
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
^ Ok, that's a good idea. Just out of curiosity though, is there no way to make an array that holds arrays of different types? What if I got the memory address of each array and saved as an int or something, which would then go into an int[]?
- 04-06-2011, 06:22 AM #7
There is but I am reluctant to tell you as once again it is a bad idea.
- 04-06-2011, 06:27 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Alright. Thanks for the quick help, anyway. Creating the "Arsenal" class was a better solution than what I had in mind.
- 04-06-2011, 06:37 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Forward references
By Norm in forum Advanced JavaReplies: 2Last Post: 06-30-2010, 02:19 AM -
Soft References
By rickcharles_b in forum Advanced JavaReplies: 0Last Post: 06-20-2007, 08:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks