gpt4 book ai didi

java - 尝试打印转置的二维数组时出错

转载 作者:行者123 更新时间:2023-11-30 02:40:53 25 4
gpt4 key购买 nike

在编写代码时,我陷入困境,我试图返回新的转置数组并实际转置数组本身。我收到错误无法将 int 转换为 int[][]。我认为 trans 将是一个数组变量。问题代码位于底部。任何帮助是极大的赞赏。

 package workfiles;



import java.util.*;
import java.util.Scanner;

public class hw2 {

// Do not modify this method
public static void main(String[] args) {

try
{
int [][] iArray = enter2DPosArray();
System.out.println("The original array values:");
print2DIArray(iArray);
int [][] tArray = transposition(iArray);
System.out.println("The transposed array values:");
print2DIArray(tArray);
}

catch (InputMismatchException exception)
{
System.out.println("The array entry failed. The program will now halt.");
}

}

// A function that prints a 2D integer array to standard output
// It prints each row on one line with newlines between rows
public static void print2DIArray(int[][] output) {

for (int row = 0; row < output.length; row++) {
for (int column = 0; column < output[row].length; column++) {
System.out.print(output[row][column] + " ");
}
System.out.println();
}
}




// A function that enters a 2D integer array from the user
// It raises an InputMismatchException if the user enters anything other
// than positive (> 0) values for the number of rows, the number of
// columns, or any array entry
public static int[][] enter2DPosArray() throws InputMismatchException {

int row=0;
int col=0;
int arow=0;
int acol=0;
int holder;
Scanner numScan = new Scanner(System.in);

while (row<=0){
System.out.print("How many rows (>0) should the array have? ");
row = numScan.nextInt();
}

while (col<=0){
System.out.print("How many columns (>0) should the array have? ");
col = numScan.nextInt();
}
int[][] iArray = new int[row][col];

while (arow < row) {

while (acol < col) {
System.out.println("Enter a positive (> 0) integer value: ");
holder = numScan.nextInt();
iArray[arow][acol] = holder;
acol++;
}

if (acol >= col) {
acol = 0;
arow ++;

}



}
//arrayName[i][j]
numScan.close();
return iArray;
}


//!!! problem code here!!!
public static int[][] transposition(int [][] iArray) {

int m = iArray.length;
int n = iArray[0].length;

int trans[][];

for(int y = 0; y<m; y++){
for(int x = 0; x<n; x++){
trans = iArray[y][x] ;
}

}
return trans;
}

}

最佳答案

你错过了两件事

1.) trans 的初始化

    int trans[][]= new int [n][m];

2.) trans 是一个二维数组

   trans[y][x] = iArray[y][x] ;
//trans = iArray[y][x] ; error

更新:为了形成这个逻辑,我们需要像这样的索引映射

  //    trans                       iArray  
// assign values column-wise row-wise
// trans[0][0] <= iArray[0][0]
// trans[1][0] <= iArray[0][1]
// trans[2][0] <= iArray[0][2]

意思是按行遍历iArrays并按列向trans数组赋值

int m = iArray.length;
int n = iArray[0].length;
// iArray[2][3]

int trans[][] = new int[n][m];
// 3 2
for(int y = 0; y<m; y++){
for(int x = 0; x<n; x++){
trans[x][y] = iArray[y][x] ;
}

}

关于java - 尝试打印转置的二维数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41749129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com