问题描述
编写一个程序,提示用户输入一个双精度值的nxn矩阵,并显示一个新矩阵,该矩阵对初始矩阵的各列进行了排序.您可以使用任何排序算法来解决该问题;请在代码标头中指定使用的排序算法的名称.您的程序必须实现排序算法;您不能使用Array类中提供的排序方法.排序应实现为一种方法,该方法将返回一个新数组,而原始数组则保持不变:
Write a program that prompts the user to enter a nxn matrix of double values and displays a new matrix which has the columns of the initial matrix sorted. You may use any sorting algorithm to solve the problem; please specify the name of the used sorting algorithm into your code header. Your program must implement a sorting algorithm; you cannot use the sorting methods provided in the Array class. The sorting should be implemented into a method, in which a new array is returned and the original array is intact:
public static double[][] sortCol(double[][] a)
程序还应该实现一种将初始矩阵和结果矩阵打印给用户的方法.打印输出应格式化良好.这是一个示例运行:
The program should also implement a method that prints the initial and the result matrices to user. The print out should be nicely formatted. Here is a sample run:
What is the dimension of matrix? 3
Enter a 3x3 matrix row by row:
0.15 0.875 0.375
0.55 0.005 0.225
0.30 0.12 0.4
The column sorted array is:
0.15 0.005 0.225
0.3 0.12 0.375
0.55 0.875 0.4
这就是我所拥有的.我相信这几乎是完美的.我认为我使用的排序方法将对列进行排序,但也可能对行进行排序.但是,当我运行程序时,我得到了...
This is what I have. I believe it is almost perfect. The sorting method I used I think will sort the column but it may also be sorting the rows. However when I run the program I get this...
任何想法/帮助.
import java.util.Scanner;
public class sdfjasdf {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("What is the dimension of your matrix?");
int matrixdim = input.nextInt();
double[][] matrix = new double[matrixdim][matrixdim];
System.out.println("Enter " + matrixdim + " rows, and " + matrixdim
+ " columns.");
Scanner input1 = new Scanner(System.in);
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix.length; column++)
matrix[row][column] = input1.nextDouble();
}
System.out.println(sortCol(matrix));
}
public static double sortCol(double[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
double currentMin = matrix[i][0];
int currentMinIndex = i;
for (int j = i; j < matrix.length; j++) {
if (currentMin > matrix[j][0]
|| (currentMin == matrix[j][0] && matrix[currentMinIndex][1] > matrix[j][1])) {
currentMin = matrix[j][0];
currentMinIndex = j;
}
}
if (currentMinIndex != i) {
double temp0 = matrix[currentMinIndex][0];
double temp1 = matrix[currentMinIndex][1];
matrix[currentMinIndex][0] = matrix[i][0];
matrix[currentMinIndex][1] = matrix[i][1];
matrix[i][0] = temp0;
matrix[i][1] = temp1;
}
}
return sortCol(matrix);
}
}
推荐答案
我怀疑您的语言环境可能需要逗号而不是浮点数格式的点.尝试将数据更改为
I suspect that your locale can require commas instead dots in float number format. Try changing your data to
0,15 0,875 0,375
0,55 0,005 0,225
0,30 0,12 0,4
如果是这样,但您希望(或必须)使用点而不是逗号,则可以通过调用
If that is true but you prefer to (or must) use dots instead of comma you can change locale used in Scanner by invoking
input.useLocale(new Locale("en", "US"));
或在使用
Locale.setDefault(new Locale("en", "US"));
sortCol
的返回类型也应为
Also return type of sortCol
should be aether
-
double[][]
,如果您想返回数组的排序后的副本(不更改原始副本).在这种情况下,您需要先 -
void
,如果您想对原始数组进行排序(因为使用它作为方法参数,您不必返回对已有对象的引用)
double[][]
in case you want to return sorted copy of array (without changing original one). In that case you will need to first create copy of original arrayvoid
in case you want to sort original array (you don't have to return reference to object that you already have since you used it as methods argument)
现在,您尝试通过再次调用sortCol(matrix)
来返回double
,因此它再次尝试返回sortCol(matrix)
(依此类推),该结果将导致stack overflow
.
Right now you are trying to return double
by invoking again sortCol(matrix)
, so it again will try to return sortCol(matrix)
(and so on) which will lead to stack overflow
.
这篇关于java二维数组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!