Answer to Question #309536 in Java | JSP | JSF for Kyle

Question #309536

1. Write a Java program without using a second array to reverse elements of each row of a 2-d array of integer values.

e.g.     1    2   3    4 4    3    2   1

5    6   7    8        8    7    6   5

            9   10  11 12 12  11  10  9



1
Expert's answer
2022-03-13T01:11:17-0500
     public static void main(String []args){
        int arr [][] = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length/2; j++) {
                int tmp = arr[i][j];
                arr[i][j] = arr[i][arr[i].length - j - 1];
                arr[i][arr[i].length - j - 1] = tmp;
            }
        }
        
        for(int i = 0; i < arr.length; i++) {
            for(int j = 0; j < arr[i].length; j++) {
                System.out.print(arr[i][j]);
            }
            
            System.out.println();        
        }
     }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS