数字列表排序
Sorting list of numbers
我正在尝试将数字列表从小到大排序并打印出来。我试过两件事:
1.
public class Sorter {
public static void main(String[] args) {
int[] numbers = {1, 3, 8, 2, 5, -2, 0, 7, 15};
int[] sorted = new int[numbers.length];
for (int a = 0; a < numbers.length; a++) {
int check = 0;
for (int b = 0; b < numbers.length; b++) {
if (numbers[a] < numbers[b]) {
check++;
}
}
sorted[check] = numbers[a];
}
for (int c = numbers.length - 1; c >= 0; c--) {
System.out.print(sorted[c] + ", ");
}
}
}
这个东西可以用,但不能用重复的值,所以我试了另一个东西
public class Sortertwo {
public static void main(String[] args) {
int[] numinput = {3, 2, 1, 4, 7, 3, 17, 5, 2, 2, -2, -4};
int[] numsorted = new int[numinput.length];
int n = 0;
for (; n < numinput.length; ) {
for (int b = 0; b < numinput.length; b++) {
int check = 0;
for (int c = 0; c < numinput.length; c++) {
if (numinput[b] <= numinput[c]) {
check++;
}
}
if (check >= (numinput.length - n) && numinput[b] != 0) {
numsorted[n] = numinput[b];
numinput[b] = 0;
n++;
}
if (n >= (numinput.length)) {
break;
}
}
}
for (int g = 0; g < numinput.length; g++) {
System.out.print(numsorted[g] + ", ");
}
}
}
它依赖于一旦使用第一个数组中的数字(找到最小的一个),当程序下次遍历数组时必须忽略它。
我试着像 null
值一样给它赋值,但它不起作用,所以我把它赋给零然后忽略它,这是一个问题,因为列表中不能有零。
有没有更好的方法来解决这个问题?谢谢
您可以随时使用:
Arrays.sort(numbers);
如果您想使用第一种方法,请更改此设置:
if (numbers[a] < numbers[b])
{
check++;
}
至:
if (numbers[a] <= numbers[b])
{
check++;
}
除非这是作业,否则按照评论建议使用 Arrays.sort
,应该是正确的方法
import java.util.Arrays;
public class S {
public static void main(String ... args) {
int[] numbers = {1, 3, 8, 2, 5, -2, 0, 7, 15};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}
打印:
[-2, 0, 1, 2, 3, 5, 7, 8, 15]
我正在尝试将数字列表从小到大排序并打印出来。我试过两件事:
1.
public class Sorter {
public static void main(String[] args) {
int[] numbers = {1, 3, 8, 2, 5, -2, 0, 7, 15};
int[] sorted = new int[numbers.length];
for (int a = 0; a < numbers.length; a++) {
int check = 0;
for (int b = 0; b < numbers.length; b++) {
if (numbers[a] < numbers[b]) {
check++;
}
}
sorted[check] = numbers[a];
}
for (int c = numbers.length - 1; c >= 0; c--) {
System.out.print(sorted[c] + ", ");
}
}
}
这个东西可以用,但不能用重复的值,所以我试了另一个东西
public class Sortertwo {
public static void main(String[] args) {
int[] numinput = {3, 2, 1, 4, 7, 3, 17, 5, 2, 2, -2, -4};
int[] numsorted = new int[numinput.length];
int n = 0;
for (; n < numinput.length; ) {
for (int b = 0; b < numinput.length; b++) {
int check = 0;
for (int c = 0; c < numinput.length; c++) {
if (numinput[b] <= numinput[c]) {
check++;
}
}
if (check >= (numinput.length - n) && numinput[b] != 0) {
numsorted[n] = numinput[b];
numinput[b] = 0;
n++;
}
if (n >= (numinput.length)) {
break;
}
}
}
for (int g = 0; g < numinput.length; g++) {
System.out.print(numsorted[g] + ", ");
}
}
}
它依赖于一旦使用第一个数组中的数字(找到最小的一个),当程序下次遍历数组时必须忽略它。
我试着像 null
值一样给它赋值,但它不起作用,所以我把它赋给零然后忽略它,这是一个问题,因为列表中不能有零。
有没有更好的方法来解决这个问题?谢谢
您可以随时使用:
Arrays.sort(numbers);
如果您想使用第一种方法,请更改此设置:
if (numbers[a] < numbers[b])
{
check++;
}
至:
if (numbers[a] <= numbers[b])
{
check++;
}
除非这是作业,否则按照评论建议使用 Arrays.sort
,应该是正确的方法
import java.util.Arrays;
public class S {
public static void main(String ... args) {
int[] numbers = {1, 3, 8, 2, 5, -2, 0, 7, 15};
Arrays.sort(numbers);
System.out.println(Arrays.toString(numbers));
}
}
打印:
[-2, 0, 1, 2, 3, 5, 7, 8, 15]