reverse two dimensional arrays in java

reverse two dimensional arrays in java

reverse array elements in two dimensional array such that the last element becomes the first

View Answers

November 17, 2010 at 2:42 PM

Hi Friend,

Try the following code:

import java.util.*;

public class TDArray { public static void reverse(int[][] arr){

 int rows = arr.length;
    int cols = arr[0].length;
    int array[][]=new int[rows][cols];
    for(int i = rows-1; i >= 0; i--) {
        for(int j = cols-1; j >= 0; j--) {
            array[rows-1-i][cols-1-j] = arr[i][j];
        }
    }
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < cols; j++) {
            System.out.print(array[i][j]+" ");
            }
     }
}

public static void main(String[] args) throws Exception {
    int rows, cols;
    int[][] arr;
    Scanner input = new Scanner(System.in);
    System.out.print("Enter number of rows greater than 20: ");
    rows = input.nextInt();
    System.out.print("Enter number of columns greater than 20: ");
    cols = input.nextInt();
    arr = new int[rows][cols];
    System.out.println("Enter elements of Array");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            arr[i][j] = input.nextInt();
        }
    }
    System.out.println("Array is: ");
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
        System.out.print(arr[i][j]+" ");
        }
    }
    System.out.println();
    reverse(arr);

        }

}

Thanks









Related Tutorials/Questions & Answers:
reverse two dimensional arrays in java
reverse two dimensional arrays in java  reverse array elements in two dimensional array such that the last element becomes the first
Two-dimensional arrays
Two-Dimensional Arrays       Two-dimensional arrays are defined as "an array of arrays"... of arrays of  ints". Such an array is said to be a two-dimensional array. 
Advertisements
reverse arrays in java
reverse arrays in java  how do i make a code that can be used to reverse the elements of an array in two dimension such that the last element of an array becomes the first element of the array and the first element of an array
reverse arrays in java
reverse arrays in java  how do i write a program in array of size n*m where both n and m are greater than 20 such that the 1st element of an array becomes the last and vice verse
two dimensional - Java Beginners
two dimensional  write a program to create a 3*3 array and print the sum of all the numbers stored in it.  Hi Friend, Try the following code: import java.io.*; import java.util.*; public class matrix
intersection of two java arrays
intersection of two java arrays  I am trying to figure out how to compare two arrays and get the intersection and put this into a third array of the correct size. I know how to copy the two arrays into the third array but am
How to concatenate two arrays in Java?
How to concatenate two arrays in Java?  How to concatenate two arrays in Java
Two- Dimensional Array - Java Beginners
Two- Dimensional Array  I am new in java programming. I am creating a two-dimensional array. This is my code ** class BinaryNumbers { public static void main(String[] args) { //create a two-dimensional array int ROWS = 21
php two dimensional array
php two dimensional array  php two dimensional array example
Two dimensional array in java
Two dimensional array in java. In this section you will learn about two-dimensional array in java with an example. As we know that array is a collection... dimensional array is defined as an "array of array". In java the element
two dimensional array
two dimensional array  how tow dimensional array works.How those loopes get incremented .and how every time the value of k changes
Multi-dimensional arrays
; The Java programming language does not really support multi-dimensional arrays. It does, however, supports an array of arrays. In Java, a two-dimensional array... Multi-dimensional arrays      
Compare two Byte Arrays?
Compare two Byte Arrays?  Compare two Byte Arrays
Two Dimensional array program
Two Dimensional array program  consider a two dimensional array of size mn where both mn are greater than 20.derive a function that can be used to elements of the array such that the last element become the first one
Two Dimensional Array Program
Two Dimensional Array Program    ... will learn how to display arrange form of two dimensional array program... a integer for array declaration Two dimensional array program. We are going
combine two arrays in php
combine two arrays in php  combine two arrays in php   $newArray = array_combine($diff, $pages_name['data']); foreach ($newArray as $data) { var_dump($data); exit('zzzzz'); echo $data . '<br>
Two Dimensional Array Program
Two Dimensional Array Program Using Nested for loop...; program . In this session we will teach how to use of a two dimensional array... are going to make two dimensional array having three row and three columns. 
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
swap two integer arrays
swap two integer arrays  int [] array1 = {1,2,3,4,5} int [] array2 = {6,7,8,9,10} how can we swap the values of bith integer arrays results like this array1 = {6,7,8,9,10} array2 = {1,2,3,4,5
how do i begin a two dimensional array?
how do i begin a two dimensional array?  I'm new to java programming and need to create a two dimensional array that enters exactly what is entered in the first dimension and then the first non-white space character of what
Merging Two Arrays Of Different Lengths
Merging Two Arrays Of Different Lengths  I have two arrays of different lengths and wants to have merged values into third. The only condition is, I want unique values in it(third array). Thanks In Advance
Need coding help - two dimensional matrix and it returns back a boolean.
Need coding help - two dimensional matrix and it returns back a boolean.  I need to write a code method that accepts a two dimensional integer arrays as the argument and returns true if the array or matrix is left or right
Two Dimensional Array Program Using Nested For Loop
Two Dimensional Array Program Using Nested For Loop ... of Java. In this lesson we will learn how to display arrange form of two...;. We are going to make a integer for array declaration Two dimensional array
Square Elements of Two Dimensional Array
Square Elements of Two Dimensional Array        This is a simple java program for implementing the two dimensional array program and its square. This session provide you
Arrays
Arrays  Write a program in java(BlueJ) to store the numbers in single dimensional array(S.D.A)into another S.D.A in reverse order of the location
Arrays in java
Arrays in java  what is param array in java
Java with Arrays
Java with Arrays  I was given the assignment to create two parallel arrays; both of size 50 (declares 50 as a constant) One is an array of strings... and store it in the arrays. The input for the problem should be provided in a text
Declare string array in Java (one and two dimensional)
Declare string array in Java (one and two dimensional... dimensional array and two dimensional as well. 1. String arr[] = new String...] + " "); } //declare and initialize two dimensional array
Reverse
Reverse  How to reverse two digits in javaprogramming
Reverse
Reverse  How to reverse two digits in javaprogramming
Introduction to java arrays
; The Java programming language does not really support multi-dimensional arrays. It does, however, supports an array of arrays. In Java, a two-dimensional array ' x... Two-dimensional arrays Two-dimensional arrays are defined as "an array
java arrays
java arrays  can i know how can we intilize the three dimentional arrays in java? and how can we assign thae values to that array
java arrays
java arrays  i need a java program to store student details like id,name,addr,marks,average,total..using arrays..input data using scanner class and by using class, object and constructor
arrays in java - Java Beginners
arrays in java  Hi All, I have two arrays. in this two array some name are same. I want to merge those arrays into single. But while merging I want to delete duplicate entries. How merge those arrays. Thanks, mln15584
java arrays
java arrays  how do you write the code for multipliying 2 double arrays to be in a 3rd double array? here is my code: package employeepay; /** * * @author Owner */ public class Main {ADS_TO_REPLACE_1 /** * @param args
About Java arrays - Java Beginners
About Java arrays  Hello.........My question is can we create a two dimensional matrix using only one for loop
Dividing Element of Two Dimensional Array
Java program.  The java two dimensional array program is operate the two... Dividing  Element of Two Dimensional Array... divide of two matrix. First all to we have to define class "
one dimensional array using java
one dimensional array using java  design and implement a java program that will read a file containing numbers and compute the following statistics: the range(low,high) the average and the median
Java Arrays
Java Arrays  import java.util.ArrayList; public class try3{ public static void reduce(int[]arr, int len){ for (int i= 0; i<len;i++) { arr[i]--; } len--; } public static void main (String[]args){ int
java arrays
java arrays  Suppose that you are required to store information about students for the module Data structures and Algorithms. The information for each student consists of the studentââ?¬â?¢s ID number, surname, other names
Introduction to Java Arrays
the one-dimensional and two-dimensional arrays. To store data in more dimensions.... In Java, a two-dimensional array ' x' is an array of one-dimensional array : ... operator. ADS_TO_REPLACE_14 Two-dimensional arrays Two-dimensional arrays
Introduction to java arrays
the one-dimensional and two-dimensional arrays. To store data in more dimensions.... In Java, a two-dimensional array ' x' is an array of one-dimensional array : ... operator. ADS_TO_REPLACE_14 Two-dimensional arrays Two-dimensional arrays
java; arrays - Java Beginners
java arrays example  How can you create a program, by using arrays and the output would be X. by using char or string.Thank you
Multidimensional Array Java
to store it in an array. We use mostly two types of arrays that is simple array... used two dimensional array. A two dimensional array can be thought as a grid...Multidimensional Array Java     
arrays
Store a table with students and information (name, ID, password, crypted password, etc) in a multi-dimensional array "stud"  Arrays and Strings..., etc) in a multi-dimensional array "stud". Output the stud table in form
Java Arrays Tutorial
Dimensional Array Two-dimensional arrays are defined as " an array... far we have studied about the one-dimensional and two-dimensional arrays... Java Arrays Tutorial      
Matrix multiplication in java
multiplication of two matrices. In java this is a simple program to multiply two matrices, we have to take two-dimensional array and the result should be saved in third two-dimensional array. Here we are going to develop a Java code for matrices
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer

Ads