Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By JosAH

Thread: 2D array

  1. #1
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default 2D array

    hello,

    how can i create a 2d array which is 25x25
    and i want it to have the following values

    row 1 empty
    row 2 ( ,x, ,x, ,x)
    row 3 (x, ,x,x, , , x)
    i will figure out the rest myself, please just show me how to create and input few values or how i can leave it empty

    thnx

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D array

    I'd do it like this:

    Java Code:
    char[][] dim2array= {
      "xxxxx".toCharArray(),
      "x   x".toCharArray(),
      "x x x".toCharArray(),
      "x   x".toCharArray(),
      "xxxxx".toCharArray()
    };
    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: 2D array

    What data type is the array?
    What do the empty slots represent if the array is of primitives?
    Go to this site and Find Arrays : Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
    for a tutorial about using arrays in java.

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

    Default Re: 2D array

    that didn't work,
    i think i may have not been clear, this is what im looking for, but i can't seem to get it to work, i need to make a 2d (25x25) array that prints the following

    but the grids don't have to be show.

    thank you for all your help

    2D array-capture.jpg

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: 2D array

    What data type is the array?
    What goes in the empty elements?
    To define a two dim array:
    <datatype>[][] twoDim = new <datatype>[rows][columns];
    Then go thru the rows and columns to set the values.
    Or another way is to only define the rows initially and define the columns later:
    <datatype>[][] twoDim = new <datatype>[rows][];
    twoDim[i] = new <datatype>[columns for this row]; // assign the columns for this row

  6. #6
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default Re: 2D array

    its suppose to be characters, first and last row are empty as well as first and last column.

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: 2D array

    There is no such thing as an empty element. The element has some value like null or 0.
    The empty elements would be past the end of the array, like the 11th element in an 10 element array.
    Or I suppose a String of length 0 could be considered empty, but it's still a String.

  8. #8
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default Re: 2D array

    ok so lets say it is null,
    so how do i do it so it would exactly show as the picture i inserted above, without the grid lines of course

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: 2D array

    We've given some examples. What have you tried?

    There are no null char values. For null, you will have to have a class as the data type of the array.

  10. #10
    aramiky818 is offline Member
    Join Date
    Apr 2011
    Posts
    14
    Rep Power
    0

    Default Re: 2D array

    please just show me how i can print the above image in 2d array.

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,266
    Rep Power
    25

    Default Re: 2D array

    See the Arrays toString() method for an easy way to display the contents of an array.
    If you want to control the output use a nested loop: (outer by row; inner by column) and use the print method for the columns in a row and the println once at the end of a row.

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,309
    Blog Entries
    7
    Rep Power
    20

    Default Re: 2D array

    Quote Originally Posted by aramiky818 View Post
    that didn't work,
    How strange because it works perfectly fine on my machine; define 'doesn't work' please.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  2. Replies: 23
    Last Post: 09-07-2010, 08:12 PM
  3. Replies: 2
    Last Post: 09-06-2010, 01:03 AM
  4. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  5. Replies: 1
    Last Post: 03-31-2009, 06:40 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
  •