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

    Default Multidimensional Array

    Need help with this multidimensional array
    Java Code:
    public int [][][] blocks = new int[1,2,3,4,5][1,2,3,4,5][1,2];
    Heres what it says...
    incompatible types
    required: int[][][]
    found: int[]

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

    Default Re: Multidimensional Array

    That's not how you initialize arrays in Java and you'll want to re-read your tutorial.

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

    Default Re: Multidimensional Array

    Last edited by TyCox94; 09-25-2011 at 06:42 AM.

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

    Default Re: Multidimensional Array

    Quote Originally Posted by TyCox94 View Post
    You're not doing what the tutorial is telling you to do. Look again. I'm not even sure what you're trying to do as your code is a bit strange.

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

    Default Re: Multidimensional Array

    Ok I see. Here is my array now:

    Java Code:
    public int[][] blocks = {{1,2,3,4,5},{1,2,3,4,5},{1,2}};
    how do i set value in an array? Something like...

    Java Code:
    blocks[1][1][1] = x;
    and
    Java Code:
    blocks[1][1][2] = y;

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

  7. #7
    Join Date
    Sep 2011
    Location
    Mumbai, India
    Posts
    35
    Rep Power
    0

    Default Re: Multidimensional Array

    Quote Originally Posted by TyCox94 View Post
    Ok I see. Here is my array now:

    Java Code:
    public int[][] blocks = {{1,2,3,4,5},{1,2,3,4,5},{1,2}};
    The above was 2 dimensional array being initialized, i think you are asking how to initialize a 3 dimensional array
    Java Code:
    int[][][] arr= {
                        {
                           { 1,2,3},
                           { 4,5,6}
                        },
                        {
                           { 7,8,9},
                           { 10,11}
                        }
                     }
    Last edited by Rameshwar Soni; 09-25-2011 at 10:43 AM.

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

    Default Re: Multidimensional Array

    How would i call a specific dimension of the array?
    Java Code:
    System.out.println(arr[1][1][1][1]);
    Last edited by TyCox94; 09-25-2011 at 11:38 PM.

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

    Default Re: Multidimensional Array

    Your array has only two dimensions, and so you will need to use only two square brackets after the variable name. e.g.,

    Java Code:
    System.out.println(arr[0][0]);
    System.out.println(arr[0][1]);
    System.out.println(arr[0][3]);
    System.out.println(arr[1][0]);
    //....
    Note that often when printing out the complete contents of an array we use a for loop. For a 2-dimensional array, we'll often use two for loops, one nested inside of the other. I believe your tutorials show something like this.

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

    Default Re: Multidimensional Array

    Why does

    Java Code:
    System.out.println(arr[0][0]);
    return

    [I@5c28305d
    EDIT: Its actually returning difference values...? This time...
    [I@6691177
    Last edited by TyCox94; 09-25-2011 at 11:52 PM.

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

    Default Re: Multidimensional Array

    Then you must have an array of more than 2 dimensions. You appear to have a 3-dimensional int array. You will need to use a 3rd index then.

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

    Default Re: Multidimensional Array

    Oh.... Sorry, thanks for all your help!

Similar Threads

  1. Multidimensional char array
    By leepikamukharji in forum New To Java
    Replies: 18
    Last Post: 04-13-2011, 02:18 PM
  2. Multidimensional Array help
    By Gaebril in forum New To Java
    Replies: 1
    Last Post: 04-09-2011, 09:36 PM
  3. Multidimensional array - swap
    By BeginnerNoob in forum New To Java
    Replies: 13
    Last Post: 03-30-2011, 04:02 PM
  4. initialize multidimensional array
    By Aidoru in forum New To Java
    Replies: 2
    Last Post: 12-04-2010, 02:00 PM
  5. [SOLVED] Multidimensional array
    By Torgero in forum New To Java
    Replies: 20
    Last Post: 03-23-2009, 12:36 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
  •