# 2D array - how do I create one with 20 rows and columns?

• 05-16-2011, 04:59 AM
2D array - how do I create one with 20 rows and columns?
Hi everyone,

I'm really new to java and I'm not comfortable with it AT ALL.
I need to create a 2d array that has rows and columns in it. I've got a gridsize of 20 x 20. I'm just a bit confused as to how to declare the array in the first place: I've got this below at the moment:

int [][] hits = new int [20][20]

But I thought it might need to have something like [COL_ARRAY][ROW_ARRAY] and I declare the two arrays first.

Totally, totally confused. I need to use the array to jump through the grid and pick up random samples of each cell later on (which I think I've got under control).

Any help is good help!! Thanks!!
• 05-16-2011, 05:23 AM
camickr
> I've got this below at the moment:

Does it work? What problems are you having?
• 05-16-2011, 05:28 AM
Junky
Code:

`int [][] hits = new int [20][20]`
This is correct.
Quote:

But I thought it might need to have something like [COL_ARRAY][ROW_ARRAY] and I declare the two arrays first.
This makes no sense. A 2D array is simply a 1D array of arrays. So what you actually have is 20 rows in your 2D array. Therefore you can do something like this:
Code:

```int[] temp = {1,2,3,4}; int[][] matrix = new int[4][4]; matrix[0] = temp; matrix[1] = temp; matrix[2] = temp; matrix[3] = temp;```
Or
Code:

```int[][] matrix = new int[4][4]; for(int x = 0; x < matrix.length; x++) {     for(int y = 0; y < matrix[x].length; y++) {         matrix[x][y] = x * y;     } }```
• 05-16-2011, 05:46 AM
Okay, thanks; good to know I had it in the first place!! I've been tidying up my program and getting things indented properly etc; my main problem now is that I have to set the array to 0 and it keeps telling me it's already been declared earlier. this is what I have as pseudocode:

For column = 0 to gridsize -1[/INDENT]For row = 0 to girdsize -1[/INDENT][/INDENT]Set hits [column][hits] to 0
Repeat samples times

Samples is a constant I've already declared, it = 100. It's the random samples in each cell that get tested.

This is what I've got; but it's not working?!?! Is it supposed to look like the last example you just posted?:-S

//set column and row for loops
for (int column = 0; column < gridSize-1; column++) { [/INDENT]for (int row = 0; row < gridSize-1; row++){
//set hits to 0
double hits = 0;
for (double hits= 0; hits < 100; hits++){
double x = minx + (column + Math.random()) * ((maxx-minx)/gridSize);
double y = miny + (row + Math.random()) * ((maxy-miny)/gridSize);
• 05-16-2011, 05:59 AM
apologies for the abysmal copy and paste; as an afterthought, can you tell me how to post indentation in codes properly too, to save headaches? :-)
• 05-16-2011, 06:02 AM
Junky
When you want to ask questions about your code you need to post the actual code. To make it readable place it between [ code ] and [ /code ] tags (without the spaces). Also post the EXACT error message you get. Do not paraphrase.

There is no need to set all the values in the array to zero. When you create an array all values are set to the default value. For primitives (int, long, double etc) the default value is zero.
• 05-16-2011, 06:26 AM
Code:

```//set column and row for loops for (int column = 0; column < gridSize-1; column++) {   [INDENT][/INDENT]for (int row = 0; row < gridSize-1; row++){ [INDENT][INDENT][/INDENT][/INDENT]double hits = 0; //set hits to 0 [INDENT][INDENT][/INDENT][/INDENT]for (double hits= 0; hits < 100 hits++){  [COLOR="Red"]- getting error here[/COLOR] [INDENT][INDENT][INDENT][/INDENT][/INDENT][/INDENT]double x = minx + (column + Math.random()) * ((maxx-minx)/gridSize); [INDENT][INDENT][INDENT][/INDENT][/INDENT][/INDENT]double y = miny + (row + Math.random()) * ((maxy-miny)/gridSize);```
My indentation isn't coming through correctly; basically everything after the second line is indented again, with 'x' and 'y' indented one more place over after that (last two lines of code). This isn't the whole code, but it's the bit that isn't compiling - everything else is working.

I'm getting an 'array is already defined' error on the extra loop I've placed in there that relates exactly to the pseudocode I pasted above. I should also explain that I'm writing this based on a pseudocode that was given to me, not that I created. I know it's probably really simple, but I've been staring at it for so long I can't see the problem anymore. Help!!! :-) Thank you!!
• 05-16-2011, 06:32 AM
Junky
You are declaring 2 variables with the same name: hits. You cannot do this in the same scope.
• 05-16-2011, 06:35 AM
camickr
Code:

`for (double hits= 0; hits < 100 hits++){`
You are missing the ";" after the "100"

Why are you using a "double" for a loop counter? Generally you would use an "int".

Also, there is no need to define the loop variable separately since you are defining the variable inside the for loop.