Results 1 to 8 of 8
  1. #1
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default Mulit dimensional Array

    Hi all,

    I have a multi-dimensional array that i want to assign variables to certain parts however, this code is not working where have i gone wrong?

    Java Code:
    String[][][] DetailsArray = new String[100][100][100];
    DetailsArray[i][][] = surname;
    DetailsArray[][i][] = forename;
    DetailsArray[][i][] = age.toString();
    regards,

    Jon

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    In that you need to provide values for the other indexes as well. I.E.
    Java Code:
    String[][][] DetailsArray = new String[100][100][100];
    DetailsArray[i][x][y] = surname;
    DetailsArray[x][i][y] = forename;
    DetailsArray[x][i][y] = age.toString();

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default

    Hi but i want assign certain text to certain elements of the array so

    Java Code:
    String[][][] DetailsArray = new String[100][100][100];
    DetailsArray[i][i][i] = {surname, forename, age.toString()};
    Could i do this?

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by jonsamwell View Post
    Hi but i want assign certain text to certain elements of the array so

    Java Code:
    String[][][] DetailsArray = new String[100][100][100];
    DetailsArray[i][i][i] = {surname, forename, age.toString()};
    Could i do this?
    Well, what you're showing is a single three element array, whereas what you've declared is a three dimensional array. Think of a cube, as that is how it would lay out. The first [] signifies the top to bottom axis along the left hand side. The second [] signifies the left to right axis along the top, and the third [] signifies the front to back axis (again, along the top).

    What exactly is it you want to store? Do you want to store a list of information consisting of three columns? I.E. a list of names combined with ages? If so, you need a two dimensional (not three) array with the second dimension being 3 (I.E. new String[100][3] to store 100 three column items) and the info above (for the sake of posterity assuming it is the fifth line in the list) would be either
    Java Code:
    DetailsArray[4] = new String[] {surname, forename, age.toString()};
    // or
    DetailsArray[4][0] = surname;
    DetailsArray[4][1] = forename;
    DetailsArray[4][2] = age.toString();
    Assuming surname and forname are variables.

  7. #7
    jonsamwell is offline Member
    Join Date
    Aug 2008
    Posts
    11
    Rep Power
    0

    Default

    Brilliant Thanks!!!

    That is exactly what i need!!

    Regards,

    Jon

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. assign a value to two dimensional array
    By makpandian in forum New To Java
    Replies: 4
    Last Post: 10-05-2011, 05:21 AM
  2. two-dimensional array
    By kHim in forum New To Java
    Replies: 4
    Last Post: 11-16-2008, 07:21 PM
  3. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  4. How to initialize a two dimensional Array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 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
  •