java Java从最小到最大对数组进行排序的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25284516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Method to sort an array from least to greatest Java
提问by Panthy
Hi I am trying to create a method that finds the mode of an array. To do this I first need to create a method that sorts the list from least to greatest but I am not sure what I am doing wrong in the first place
嗨,我正在尝试创建一种查找数组模式的方法。为此,我首先需要创建一个方法,将列表从最小到最大排序,但我不确定我首先做错了什么
public class Mode {
public int [] sort(int[] asd) {
int[] sorted = new int[10];
for (int i = 0; i < asd.length; i++) {
for (int j = 1; j < asd.length; j++) {
if ( (asd[i] > asd[j]) && (i != j) ) {
int temp = asd[j];
asd[j] = asd[i];
asd[i] = asd[temp];
}
else
continue;
}
}
return sorted;
}
public static void main(String[] args) {
Mode list1 = new Mode();
int[] array = {3,2,5,4,1,1,1,1,10,9};
int[] potato = list1.sort(array);
for (int i = 0; i < potato.length; i++)
System.out.print(potato[i]);
}
}
When I run this, I get 0000000000 as an output. I think there is something wrong in the method since I am returning an array that has been initialized but nothing has been put in there. How do I (in my nested for loop) add each number in the sorted array?
当我运行它时,我得到 0000000000 作为输出。我认为该方法有问题,因为我返回了一个已初始化但未放入任何内容的数组。我如何(在我的嵌套 for 循环中)添加排序数组中的每个数字?
回答by vikingsteve
There were some issues in your code, they are fixed as per below:
您的代码中存在一些问题,它们已按以下方式修复:
public class Mode {
public int [] sort(int[] asd) {
int[] sorted = asd.clone();
for (int i = 0; i < sorted.length; i++) {
for (int j = i+1; j < sorted.length; j++) {
if ( (sorted[i] > sorted[j]) && (i != j) ) {
int temp = sorted[j];
sorted[j] = sorted[i];
sorted[i] = temp;
}
}
}
return sorted;
}
public int findMode(int[] sorted) {
// do whatever you want to do here...
return 0;
}
public static void main(String[] args) {
Mode list1 = new Mode();
int[] array = {3,2,5,4,1,1,1,1,10,9};
int[] potato = list1.sort(array);
for (int i = 0; i < potato.length; i++) {
System.out.print(potato[i] + ",");
}
System.out.println();
System.out.print(list1.findMode(potato));
}
}
To explain, previously you were sorting asd
. Now we are sorting sorted
, which starts as a copy of asd
.
解释一下,之前您正在对asd
. 现在我们正在排序sorted
,它从asd
.
The loop for j
should start at i+1
.
for 循环j
应该从 开始i+1
。
The last part of your "swap" operation should be sorted[i] = temp
.
“交换”操作的最后一部分应该是sorted[i] = temp
.
回答by Prasad Khode
You can use java.util.Arraysto sort the array in ascending order
您可以使用java.util.Arrays按升序对数组进行排序
public class SortArray {
public static void main(String[] args) {
int[] array = { 3, 2, 5, 4, 1, 1, 1, 1, 10, 9 };
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
}
The output for the above code snippet will be
上述代码片段的输出将是
1 1 1 1 2 3 4 5 9 10
回答by Bruce Collie
You are assigning to elements of asd
in your sort method, then returning the array sorted
, which is never altered in your sort
method.
您asd
在 sort 方法中分配给元素,然后返回 array sorted
,它在您的sort
方法中永远不会改变。
Additionally, you should initialise your sorted
array to be the same size as asd
.
此外,您应该将sorted
数组初始化为与asd
.