2D array

Printable View

• 09-09-2011, 09:20 PM
aramiky818
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
• 09-09-2011, 09:43 PM
JosAH
Re: 2D array
I'd do it like this:

Code:

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

Jos
• 09-09-2011, 09:45 PM
Norm
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.
• 09-09-2011, 10:40 PM
aramiky818
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

Attachment 1770
• 09-09-2011, 10:49 PM
Norm
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
• 09-09-2011, 10:51 PM
aramiky818
Re: 2D array
its suppose to be characters, first and last row are empty as well as first and last column.
• 09-09-2011, 10:54 PM
Norm
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.
• 09-09-2011, 11:06 PM
aramiky818
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
• 09-09-2011, 11:08 PM
Norm
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.
• 09-09-2011, 11:31 PM
aramiky818
Re: 2D array
please just show me how i can print the above image in 2d array.
• 09-09-2011, 11:35 PM
Norm
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.
• 09-10-2011, 08:52 AM
JosAH
Re: 2D array
Quote:

Originally Posted by aramiky818
that didn't work,

How strange because it works perfectly fine on my machine; define 'doesn't work' please.

kind regards,

Jos