# 2 Dimensional Arrays: I cant manage to read/write them.

• May 2nd, 2012, 07:52 AM
seaofFire
2 Dimensional Arrays: I cant manage to read/write them.
Suppose I had an array like this. Its a 2 by 3 array.

int[] [] array = new int [2] [3] // two dimensional array

I cant manage to write on to it.

System.out.println ("Enter first integer of first colum of array");
array [i] [0] = Integer.parseInt (stdin.readLine ());

for (int j = 0 ; j <4 ; j++)
{
System.out.println (array [1] + array [2]);
}

I may be good in math, but when it comes to writing java~X(
• May 2nd, 2012, 08:04 AM
KevinWorkman
Re: 2 Dimensional Arrays: I cant manage to read/write them.
Split your problem up into smaller pieces, and concentrate on a single piece at a time. Post an SSCCE that demonstrates just that single piece, and we'll go from there.

Recommended reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
• May 2nd, 2012, 01:32 PM
aussiemcgr
Re: 2 Dimensional Arrays: I cant manage to read/write them.
How much do you understand about what 2 dimensional arrays are?

You probably know what a 1 dimensional array is. If you had a 1 dimensional array of ints with size 5 and filled with increasing numbers (0-4), we would initialize it with:
int[] array = new int[5];
... (assume we then add 0,1,2,3,4 respectfully) ...
and it would look like this:
{0,1,2,3,4}
There is another way to initialize an array when you know all the index values. That can be done by saying:
int[] array = new int[]{0,1,2,3,4};
This line also creates an array that looks like this:
{0,1,2,3,4}

Now, a 2 dimensional array is very different than a 1 dimensional array. The easiest way of thinking about 2 dimensional arrays is to think of them as a 1 dimensional array, which holds another 1 dimensional array in each of its indexes.
So, if we wanted to create a 2 dimensional array of ints with size 2 by 3, we can imagine it as being the same as creating a 1 dimensional array of size 2, where each index holds 1 dimensional int array of size 3. Here is an example of 3 ways to create, and fill, a 2 dimensional array with the same values. Looking at these 3 ways might help you visualize what a 2 dimensional array looks like:
Way #1:
Code java:

```int[][] array = new int[2][3]; array[0][0] = 0; array[0][1] = 1; array[0][2] = 2; array[1][0] = 3; array[1][1] = 4; array[1][2] = 5;```

Way #2:
Code java:

```int[][] array = new int[2][3]; int[] subArray1 = new int[3]; subArray1[0] = 0; subArray1[1] = 1; subArray1[2] = 2; array[0] = subArray1; int[] subArray2 = new int[3]; subArray2[0] = 3; subArray2[1] = 4; subArray2[2] = 5; array[1] = subArray2;```

Way #3:
Code java:

`int[][] array = new int[][]{{0,1,2},{3,4,5}};`

Now, if that all makes sense to you, then it should also be clear to you that making the call to: array[0] would return an array of ints, not an int. So in our example, it would return the array: {0,1,2}. However, the call to: array[0][0] would return just an int (0 in our case), since that statement is effectively saying: get the first element in the first array.

Does any of that help at all?