Results 1 to 4 of 4
  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Creating a 2D object array in a loop

    I have an object with two constants that I want initialising in the constructor using two parameters.

    I also want to make a 2D array of these objects.

    What is the syntax for this?

    If I type:

    Java Code:
    Obj[][] myObj = new Obj[10][10];

    Does this automatically run the constructor and assign these objects to the array? Or are the elements left empty (Just the memory allocated for the objects?)


    Can I then go on make a loop and create objects for the array in a way similar to this (Not a loop in the example):

    Java Code:
    myObj[0][0] = Obj(20, 100);
    myObj[0][0] = Obj(24, 29);
    //...

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating a 2D object array in a loop

    the elements left empty
    Yes. One quick way to tell what is there is to print one of the elements.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    226
    Rep Power
    3

    Default Re: Creating a 2D object array in a loop

    Quote Originally Posted by Norm View Post
    Yes. One quick way to tell what is there is to print one of the elements.
    Didn't think of that


    I didn't know whether arrays of objects were different to arrays of primitive types like int.
    I thought that the constructor may have automatically have been called.


    Thanks for clarifying.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating a 2D object array in a loop

    arrays of objects were different to arrays of primitive types like int.
    Arrays of objects get null values, arrays of int get 0 for their initial value.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: 04-02-2012, 01:50 PM
  2. Replies: 3
    Last Post: 01-04-2012, 02:30 PM
  3. Refer to an object from an array/for loop
    By Chro in forum New To Java
    Replies: 2
    Last Post: 11-09-2010, 06:12 PM
  4. Trouble creating object that includes array
    By Desdenova in forum New To Java
    Replies: 7
    Last Post: 05-18-2010, 07:33 PM
  5. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:14 AM

Posting Permissions

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