Results 1 to 8 of 8

Thread: Object Arrays

  1. #1
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Object Arrays

    I need to make an array of multiple objects.

    Like instead of:
    Java Code:
    public MainBot mainBotObject1 = new MainBot();
    public MainBot mainBotObject2 = new MainBot();
    public MainBot mainBotObject3 = new MainBot();
    Something like:
    Java Code:
    public MainBot mainBotObject[1] = new MainBot();
    public MainBot mainBotObject[2] = new MainBot();
    public MainBot mainBotObject[3] = new MainBot();
    Is this possible?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Object Arrays

    I looked it up and it says it needs to be declared like this...
    Java Code:
        MainBot[] mainBotObject = new MainBot[2]; 
        MainBot[0] = new MainBot();
    Error says on the second line:
    ']' expected

    invalid method declaration; return type required

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

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

    Java Code:
    MainBot[] mainBots = new MainBot[3];
    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:
    for(int i = 0; i < mainBots.length; ++i) {
        mainBot[i] = new MainBot();
    }
    Get in the habit of using standard Java naming conventions!

  5. #5
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    5

    Default Re: Object Arrays

    Quote Originally Posted by TyCox94 View Post
    invalid method declaration; return type required
    Post your code, maybe we can help with that.
    Get in the habit of using standard Java naming conventions!

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Object Arrays

    Quote Originally Posted by TyCox94 View Post
    I looked it up and it says it needs to be declared like this...
    Java Code:
        MainBot[] mainBotObject = new MainBot[2]; 
        MainBot[0] = new MainBot();
    Error says on the second line:
    Close but you're trying to use the class name where you should be using the array variable. Try:

    Java Code:
        MainBot[] mainBotObject = new MainBot[2]; 
        // MainBot[0] = new MainBot();
        mainBotObject[0] = new MainBot();
    But most importantly read the tutorials on how to use arrays as this will all be explained well there.

  7. #7
    TyCox94's Avatar
    TyCox94 is offline Member
    Join Date
    Sep 2011
    Posts
    38
    Rep Power
    0

    Default Re: Object Arrays



    This doesn't seem to be picking up the thread's(MainBot) location:

    Java Code:
    MainBot[] mainBotObject = new MainBot[2];
    mainBotObject[0] = new MainBot();
    Error:
    cannot find symbol
    symbol: class mainBotObject
    location: class MainFrame

    ']' expected

    invalid method declaration; return type required
    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default 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

  1. Help with object arrays
    By kingkongjaffa in forum New To Java
    Replies: 1
    Last Post: 08-04-2011, 04:30 PM
  2. Object Orientation - Arrays
    By Boysie in forum Jobs Discussion
    Replies: 4
    Last Post: 04-14-2010, 09:32 PM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Object Arrays
    By rsvr in forum New To Java
    Replies: 9
    Last Post: 12-05-2009, 11:27 PM
  5. Replies: 2
    Last Post: 12-10-2008, 04:40 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
  •