Home >Java >javaTutorial >Detailed explanation of examples of transposing two-dimensional arrays in Java

Detailed explanation of examples of transposing two-dimensional arrays in Java

黄舟
黄舟Original
2017-10-13 09:52:132415browse

This article mainly introduces the method of realizing two-dimensional array transposition in java. It analyzes the principle, implementation steps and related operation skills of java two-dimensional array transposition in detail in the form of examples. Friends in need can refer to it

The example in this article describes the method of transposing a two-dimensional array in Java. Share it with everyone for your reference, the details are as follows:

Here, create three classes: Test2, Exchange, and Out in the file

Write the exchange() method in the Exchange class, and create two Arrays arrayA, arrayB, arraryB[j][i]=arraryA[i][j] implements the transposition of the array.

Write the out() method in the Out class, and use a for loop to traverse the method to achieve output.

The specific code is as follows:


package Tsets;
import java.util.*;
public class Test2
{
    public static void main(String args[])
    {
        Out T1=new Out();
        Out T2=new Out();
        Exchange E=new Exchange();
        System.out.println("脚本之家测试结果:");
        System.out.println("转置前的二维数组如下:");
        T1.out(E.arraryA);
        E.exchange();
        System.out.println("转置后的二维数组如下:");
        T2.out(E.arraryB);
    }
}
//数组转置
class Exchange
{
    int arraryA[][]={{11,12,13,14,15},{21,22,23,24,25},{31,32,33,34,35},{41,42,43,44,45},{51,52,53,54,55}};
    int arraryB[][] = new int[arraryA[0].length][arraryA.length];
    public void exchange ()
    {
        for(int i=0;i<arraryA.length;i++)
        {
            for(int j=0;j<arraryA[i].length;j++)
            {
                arraryB[j][i]=arraryA[i][j];
            }
        }
    }
}
//数字循环遍历输出
class Out
{
    public void out(int c[][])
    {
        for (int i=0;i<c.length ;i++ )
        {
            for (int j=0;j<c[i].length ;j++ )
            {
                System.out.print(c[i][j]+" ");
            }
            System.out.println();
        }
    }
}

Running results:

The above is the detailed content of Detailed explanation of examples of transposing two-dimensional arrays in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn