Java:排序数组列表,return 为二维数组
Java: Sort List of a arrays and return as 2D array
在 class 中,我们的老师要求我们对数组列表进行排序,然后将其作为二维数组返回,并打印每个更改。
这是数组:35、7、63、42、24、21
最后程序应该像这样打印出来:
[7 35 63 42 24 21]
[ 7 21 63 42 24 35 ]
[7 21 24 42 63 35]
[7 21 24 35 63 42]
[7 21 24 35 42 63]
[7 21 24 35 42 63]
我有以下代码,但不知何故它根本不起作用(请注意 public static void 已经给出所以我们必须实现 public static int[][] selectionsort(int[]一)
怎么了?
public static void main(String[] args) {
int[] a = new int[] { 35, 7, 63, 42, 24, 21 };
int[][] c = selectionsort(a);
for (int i = 0; i < c.length; i++){
System.out.print("[ ");
for (int j = 0; j < c[i].length; j++) {
System.out.print(c[i][j]+" ");
}
System.out.println("]");
}
/*
* expected printout
* [ 7 35 63 42 24 21 ]
* [ 7 21 63 42 24 35 ]
* [ 7 21 24 42 63 35 ]
* [ 7 21 24 35 63 42 ]
* [ 7 21 24 35 42 63 ]
* [ 7 21 24 35 42 63 ]
*/
}
public static int[][] selectionsort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
我没有验证您实现的选择排序逻辑,但问题在任何标准编译器中都存在:类型不匹配:无法从 int[] 转换为 int[][]
你必须做的事情:你必须return选择排序算法的历史,它是一个二维数组,每一行都是选择中的一个步骤排序算法。
你做了什么:你return解决了排序算法的问题,它是一个一维数组。
你必须在每次迭代后跟踪将它放入二维数组的数组,并且return那个二维数组
public class Main {
public static void main(String[] args) {
int[] a = new int[]{35, 7, 63, 42, 24, 21};
int[][] c = selectionsort(a);
for (int i = 0; i < c.length; i++) {
System.out.print("[ ");
for (int j = 0; j < c[i].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println("]");
}
/*
* expected printout
* [ 7 35 63 42 24 21 ]
* [ 7 21 63 42 24 35 ]
* [ 7 21 24 42 63 35 ]
* [ 7 21 24 35 63 42 ]
* [ 7 21 24 35 42 63 ]
* [ 7 21 24 35 42 63 ]
*/
}
public static int[][] sort(int[] a) {
int result[][] = new int[a.length][a.length];
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
for (int k = 0; k < a.length; k++) {
result[i][k] = a[k];
}
}
return result;
}
}
在 class 中,我们的老师要求我们对数组列表进行排序,然后将其作为二维数组返回,并打印每个更改。
这是数组:35、7、63、42、24、21 最后程序应该像这样打印出来:
[7 35 63 42 24 21]
[ 7 21 63 42 24 35 ]
[7 21 24 42 63 35]
[7 21 24 35 63 42]
[7 21 24 35 42 63]
[7 21 24 35 42 63]
我有以下代码,但不知何故它根本不起作用(请注意 public static void 已经给出所以我们必须实现 public static int[][] selectionsort(int[]一)
怎么了?
public static void main(String[] args) {
int[] a = new int[] { 35, 7, 63, 42, 24, 21 };
int[][] c = selectionsort(a);
for (int i = 0; i < c.length; i++){
System.out.print("[ ");
for (int j = 0; j < c[i].length; j++) {
System.out.print(c[i][j]+" ");
}
System.out.println("]");
}
/*
* expected printout
* [ 7 35 63 42 24 21 ]
* [ 7 21 63 42 24 35 ]
* [ 7 21 24 42 63 35 ]
* [ 7 21 24 35 63 42 ]
* [ 7 21 24 35 42 63 ]
* [ 7 21 24 35 42 63 ]
*/
}
public static int[][] selectionsort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a;
}
我没有验证您实现的选择排序逻辑,但问题在任何标准编译器中都存在:类型不匹配:无法从 int[] 转换为 int[][]
你必须做的事情:你必须return选择排序算法的历史,它是一个二维数组,每一行都是选择中的一个步骤排序算法。
你做了什么:你return解决了排序算法的问题,它是一个一维数组。
你必须在每次迭代后跟踪将它放入二维数组的数组,并且return那个二维数组
public class Main {
public static void main(String[] args) {
int[] a = new int[]{35, 7, 63, 42, 24, 21};
int[][] c = selectionsort(a);
for (int i = 0; i < c.length; i++) {
System.out.print("[ ");
for (int j = 0; j < c[i].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println("]");
}
/*
* expected printout
* [ 7 35 63 42 24 21 ]
* [ 7 21 63 42 24 35 ]
* [ 7 21 24 42 63 35 ]
* [ 7 21 24 35 63 42 ]
* [ 7 21 24 35 42 63 ]
* [ 7 21 24 35 42 63 ]
*/
}
public static int[][] sort(int[] a) {
int result[][] = new int[a.length][a.length];
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
for (int k = 0; k < a.length; k++) {
result[i][k] = a[k];
}
}
return result;
}
}