Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

1. ## 2D ARRAY PROBLEM

I have a 2D array called box.

`int[][] box = new int [2][3];`

Its has elements as follows:

```box [0][0] = 1;
box [0][1] = 2;
box [0][2] = 3;
box [1][0] = 1;
box [1][1] = 2;
box [1][2] = 3;```

I want to end up after coding with elements in the array as follows:
```box [0][0] = 2;
box [0][1] = 3;
box [0][2] = 3;
box [1][0] = 2;
box [1][1] = 3;
box [1][2] = 3;```
i.e All elements in each column has had its values
replaced by values from its immediate right hand column

I have tried :
```for (int j = 0; j < box.length - 1; j++)
for (int i = 0; i < box.length - 1; i++)
box[i][j] = box[i] [j + 1];```
but no joy. Any thoughts please??

2. ## Re: 2D ARRAY PROBLEM

Originally Posted by av8
but no joy.
That provides zero information. Presumably your code didn't work. Since there is a gazillion reasons why it didn't work we have no idea what your problem is. If you provide details it will help others help you.

Hint: a 2D array is simply a 1D array of 1D arrays.
```int[][] matrix = {{1,2,3}, {4,5,6}, {7,8,9}};
for(int outer = 0; outer < matrix.length; outer++) {
int[] temp = matrix[outer];
for(int inner = 0; inner < temp.length; inner++) {
System.out.print(temp[inner] + " ");
}
System.out.println();
}```
So you can concentrate on 1 dimension instead of 2.

3. ## Re: 2D ARRAY PROBLEM

for (int j = 0; j < 2; j++)
for (int i = 0; i < 1; i++)
box[i][j] = box[i] [j + 1];

The above code is now working correctly. Thanks for commenting Junky.

kind regards av8

4. ## Re: 2D ARRAY PROBLEM

Do not hard code values like that. It will not work for a 2D array of any other size.