The problem I am having is that I must create program that uses an object to reverse the elements of each row in a 2D Array and prints out the resulting matrix. The code I have only prints out the first 3 numbers of each row in reverse. This is my code:
class Reverse {
void matrixReverse(int[][] data) {
int row_val = data.length;
int col_val = data[0].length;
int[][] data_reverse = new int[row_val][col_val];
for(int row = row_val - 1; row >= 0; row--){
for(int col = col_val - 1; col >= 0; col--){
data_reverse[row_val - 1 - row][col_val - 1 - col] = data[row][col];
}
}
for(int row = 0; row < row_val; row++){
for(int col = 0; col < col_val; col++){
System.out.println(data_reverse[row][col]);
}
System.out.println();
}
}
}
public class ArrayReverse {
public static void main(String[] args) {
int[][] data = {
{3, 2, 5},
{1, 4, 4, 8, 13},
{9, 1, 0, 2},
{0, 2, 6, 3, -1, -8}
}; // Creates the array
Reverse reverse = new Reverse();
reverse.matrixReverse(data);
}
}
The output is:
6
2
0
0
1
9
4
4
1
5
2
3
As you can see, it's only printing the first 3 numbers of the rows but I need it to print all of them.