转置2D数组仅适用于矩形数组-Java
我有一个类,它有一个方法,可以在给定数组、行和列的情况下转换数组转置2D数组仅适用于矩形数组-Java,java,arrays,multidimensional-array,Java,Arrays,Multidimensional Array,我有一个类,它有一个方法,可以在给定数组、行和列的情况下转换数组 public class Transpose { public static void main(String[] args) { int[][] array = new int[6][5]; System.out.println("Original:"); for(int i = 0; i < array.length; i++) { for(int j = 0; j &
public class Transpose {
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayRows][arrayColumns];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
}
我意识到只有当arrayRows和arrayColumns的值相同时,该方法才有效,例如:6,6或5,5。我试着将值放在彼此相反的位置,行放在列中,反之亦然,但是它不起作用。如何使该方法适用于非矩形/方形阵列?有关说明,请参阅行注释。这些是我唯一修改的行
public static void transpose(int[][] array, int arrayRows, int arrayColumns) {
int[][] transposedArray = new int[arrayColumns][arrayRows]; //swap number of rows and columns
System.out.println("Transposed:");
for (int i = 0; i < transposedArray.length; i++) { //take the length of transposedArray, not array
for (int j = 0; j < transposedArray[i].length; j++) { //take the length of transposedArray, not array
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
publicstaticvoid转置(int[][]数组,int-arrayRows,int-arrayColumns){
int[][]TransposeArray=new int[arrayColumns][arrayRows];//交换行数和列数
System.out.println(“转置:”);
对于(int i=0;i
有关说明,请参见行注释。这些是我唯一修改的行
public static void transpose(int[][] array, int arrayRows, int arrayColumns) {
int[][] transposedArray = new int[arrayColumns][arrayRows]; //swap number of rows and columns
System.out.println("Transposed:");
for (int i = 0; i < transposedArray.length; i++) { //take the length of transposedArray, not array
for (int j = 0; j < transposedArray[i].length; j++) { //take the length of transposedArray, not array
transposedArray[i][j] = array[j][i];
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
publicstaticvoid转置(int[][]数组,int-arrayRows,int-arrayColumns){
int[][]TransposeArray=new int[arrayColumns][arrayRows];//交换行数和列数
System.out.println(“转置:”);
对于(int i=0;i
您需要交换转置矩阵中arrayRows
和arrayColumns
的位置,因为新矩阵应该是[5][6]
而不是[6][5]
那么你的产品线呢
int[][] transposedArray = new int[arrayRows][arrayColumns];
变成
int[][] transposedArray = new int[arrayColumns][arrayRows];
我们还需要在下面的语句中交换i
和j
,因为循环遵循原始矩阵的索引:
transposedArray[i][j] = array[j][i];
到
最后,在创建转置矩阵时,不能像现在这样打印转置矩阵,因为这样只打印原始矩阵。我建议在创建完矩阵后再打印它
通过这些更改,您的代码将以如下方式结束:
Original:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
Transposed:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayColumns][arrayRows];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[j][i] = array[i][j];
}
}
for(int i = 0; i < transposedArray.length; i++) { //print the transposed matrix
for(int j = 0; j < transposedArray[i].length; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
publicstaticvoidmain(字符串[]args){
int[][]数组=新的int[6][5];
System.out.println(“原件:”);
for(int i=0;i
您可以看到一个工作示例。您需要交换转置矩阵中
arrayRows
和arrayColumns
的位置,因为新矩阵应该是[5][6]
而不是[6][5]
那么你的产品线呢
int[][] transposedArray = new int[arrayRows][arrayColumns];
变成
int[][] transposedArray = new int[arrayColumns][arrayRows];
我们还需要在下面的语句中交换i
和j
,因为循环遵循原始矩阵的索引:
transposedArray[i][j] = array[j][i];
到
最后,在创建转置矩阵时,不能像现在这样打印转置矩阵,因为这样只打印原始矩阵。我建议在创建完矩阵后再打印它
通过这些更改,您的代码将以如下方式结束:
Original:
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
6 6 6 6 6
Transposed:
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
public static void main(String[] args) {
int[][] array = new int[6][5];
System.out.println("Original:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
array[i][j] += i+1;
System.out.print(array[i][j] + " ");
}
System.out.println();
}
System.out.println();
transpose(array, 6, 5);
}
public static void transpose(int[][] array, int arrayRows, int arrayColumns)
{
int[][] transposedArray = new int[arrayColumns][arrayRows];
System.out.println("Transposed:");
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[i].length; j++)
{
transposedArray[j][i] = array[i][j];
}
}
for(int i = 0; i < transposedArray.length; i++) { //print the transposed matrix
for(int j = 0; j < transposedArray[i].length; j++) {
System.out.print(transposedArray[i][j] + " ");
}
System.out.println();
}
}
publicstaticvoidmain(字符串[]args){
int[][]数组=新的int[6][5];
System.out.println(“原件:”);
for(int i=0;i
您可以看到一个工作示例。for(int i=0;i
//应计算行数。=6.
正在从0开始迭代。。。五,
for(int j = 0; j < array[i].length; j++) // should count the number of columns = 5
要访问6。。。你需要j=5;i=0…4
循环无法访问(int i=0;ifor(int j = 0; j < array[i].length; j++) // should count the number of columns = 5
要访问6。。。你需要j=5;i=0…4
循环无法访问值当转置矩阵时,维度也会转置。例如,6x5矩阵的正确转置将具有5x6维度。你不是