Results 1 to 9 of 9
  1. #1
    w00tguy123 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default 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.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I have to ask why? This sounds like a bad idea.

  4. #4
    w00tguy123 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Here's an example of what I'm trying to do:

    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;
    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.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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".

  6. #6
    w00tguy123 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    ^ 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[]?

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    There is but I am reluctant to tell you as once again it is a bad idea.

  8. #8
    w00tguy123 is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Alright. Thanks for the quick help, anyway. Creating the "Arsenal" class was a better solution than what I had in mind.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can make a super class which has all the similarities of the items and then create a two d array of that super class.

Similar Threads

  1. Forward references
    By Norm in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2010, 02:19 AM
  2. Soft References
    By rickcharles_b in forum Advanced Java
    Replies: 0
    Last Post: 06-20-2007, 08:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •