7

我在java中做一个升序和降序编号,这是我的代码:

System.out.print("Enter How Many Inputs: ");
int num1 = Integer.parseInt(in.readLine());
int arr[] = new int[num1];

for (int i = 0; i<num1; i++) {
    System.out.print("Enter Value #" + (i + 1) + ":");
    arr[i] =Integer.parseInt(in.readLine());
}

System.out.print("Numbers in Ascending Order:" );

for(int i = 0; i < arr.length; i++) {
    Arrays.sort(arr);
    System.out.print( " " +arr[i]);
}

System.out.println(" ");
System.out.print("Numbers in Descending Order: " );

目前,代码生成以下内容:

Enter How Many Inputs: 5
Enter Value #1:3
Enter Value #2:5
Enter Value #3:6
Enter Value #4:11
Enter Value #5:2
Numbers in Ascending Order: 2 3 5 6 11 
Numbers in Descending Order: 

因此,该Arrays.sort(arr)调用似乎有效-但我正在寻找一种类似的简单方法来提供降序排序,但在文档中找不到它。有任何想法吗?

4

13 回答 13

7

我想到了三种可能的解决方案:

1.颠倒顺序:

//convert the arr to list first
Collections.reverse(listWithNumbers);
System.out.print("Numbers in Descending Order: " + listWithNumbers);

2. 向后迭代并打印:

Arrays.sort(arr);
System.out.print("Numbers in Descending Order: " );
for(int i = arr.length - 1; i >= 0; i--){
  System.out.print( " " +arr[i]);
}

3. 使用“相反”比较器对其进行排序:

Arrays.sort(arr, new Comparator<Integer>(){
   int compare(Integer i1, Integer i2) {
      return i2 - i1;
   }
});
// or Collections.reverseOrder(), could be used instead
System.out.print("Numbers in Descending Order: " );
for(int i = 0; i < arr.length; i++){
  System.out.print( " " +arr[i]);
}
于 2012-09-29T21:53:28.940 回答
3
public static void main(String[] args) {
          Scanner input =new Scanner(System.in);
          System.out.print("enter how many:");
         int num =input.nextInt();
    int[] arr= new int [num];
    for(int b=0;b<arr.length;b++){
   System.out.print("enter no." + (b+1) +"=");
   arr[b]=input.nextInt();
    }

    for (int i=0; i<arr.length;i++) {
        for (int k=i;k<arr.length;k++) {

        if(arr[i] > arr[k]) {

        int temp=arr[k];
        arr[k]=arr[i];
        arr[i]=temp;
        }
            }

    }
    System.out.println("******************\n output\t accending order");


    for (int i : arr){
        System.out.println(i);
    }
}
}
于 2014-06-23T07:32:43.157 回答
1

你可以让两个函数一个用于升序,另一个用于降序接下来的两个函数在将数组转换为列表后工作

public List<Integer> sortDescending(List<Integer> arr){
    Comparator<Integer> c = Collections.reverseOrder();
    Collections.sort(arr,c);
    return arr;
  }

下一个功能

public List<Integer> sortAscending(List<Integer> arr){   
    Collections.sort(arr);
    return arr;
  }
于 2013-03-15T21:29:05.720 回答
1

您为什么要使用array和烦恼第一个问题是通缉号码的数量?

首选ArrayList与相应比较器关联的一个:

List numbers = new Arraylist();
//add read numbers (int (with autoboxing if jdk>=5) or Integer directly) into it

//Initialize the associated comparator reversing order. (since Integer implements Comparable)
Comparator comparator = Collections.reverseOrder();

//Sort the list
Collections.sort(numbers,comparator);
于 2012-09-29T21:54:01.200 回答
1
int arr[] = { 12, 13, 54, 16, 25, 8, 78 };

for (int i = 0; i < arr.length; i++) {
    Arrays.sort(arr);
    System.out.println(arr[i]);
}
于 2016-12-22T11:56:36.750 回答
1
package pack2;

import java.util.Scanner;

public class group {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner data= new Scanner(System.in);
    int value[]= new int[5];
    int temp=0,i=0,j=0;
    System.out.println("Enter 5 element of array");
    for(i=0;i<5;i++)
    value[i]=data.nextInt();
     for(i=0;i<5;i++)
     {
     for(j=i;j<5;j++)
     {
      if(value[i]>value[j])
      {
       temp=value[i];
       value[i]=value[j];
       value[j]=temp;
      }
     }

     }
      System.out.println("Increasing Order:");
      for(i=0;i<5;i++)
           System.out.println(""+value[i]); 
    }
于 2016-07-14T04:19:07.600 回答
0

像以前一样对数组进行排序,但以相反的顺序打印元素,使用倒数而不是递增的循环。

此外,将排序移出循环 - 当您只需要对数组进行一次排序时,您当前正在一遍又一遍地对数组进行排序。

                Arrays.sort(arr);
                for(int i = 0; i < arr.length; i++){
                    //Arrays.sort(arr); // not here
                    System.out.print( " " +arr[i]);
                }
                for(int i = arr.length-1; i >= 0; i--){
                    //Arrays.sort(arr); // not here
                    System.out.print( " " +arr[i]);
                }
于 2012-09-29T21:46:20.083 回答
0

只需按升序对数组进行排序并向后打印即可。

Arrays.sort(arr);
for(int i = arr.length-1; i >= 0 ; i--) {
    //print arr[i]
}
于 2012-09-29T21:46:26.707 回答
0

您可以先对数组进行排序,然后在两个方向上循环两次:

Arrays.sort(arr); 
System.out.print("Numbers in Ascending Order:" ); 
for(int i = 0; i < arr.length; i++){ 
  System.out.print( " " + arr[i]); 
} 
System.out.print("Numbers in Descending Order: " ); 
for(int i = arr.length - 1; i >= 0; i--){ 
  System.out.print( " " + arr[i]); 
} 
于 2012-09-29T21:46:37.143 回答
0
Arrays.sort(arr, Collections.reverseOrder());
for(int i = 0; i < arr.length; i++){
    System.out.print( " " +arr[i]);
}

并移出Arrays.sort()那个 for 循环..您在每次迭代中对相同的数组进行排序..

于 2012-09-29T21:48:04.663 回答
0

您可以采用升序数组并以相反的顺序输出,因此将第二个 for 语句替换为:

for(int i = arr.length - 1; i >= 0; i--) {
    ...
}

如果类路径上有 Apache 的 commons-lang,它有一个可以使用的方法 ArrayUtils.reverse(int[])。

顺便说一句,您可能不想在 for 循环的每个循环中对其进行排序。

于 2012-09-29T21:53:03.277 回答
0

使用反向 for 循环以降序打印,

for (int i = ar.length - 1; i >= 0; i--) {
    Arrays.sort(ar);
    System.out.println(ar[i]);
}
于 2018-08-26T10:04:40.600 回答
-2

我以这种方式完成了它(我是java新手(也在编程中))

import java.util.Scanner;

公共类 SortingNumbers {

public static void main(String[] args) {
    Scanner scan1=new Scanner(System.in);
    System.out.print("How many numbers you want to sort: ");
    int a=scan1.nextInt();

    int i,j,k=0; // i and j is used in various loops.
    int num[]=new int[a];
    int great[]= new int[a];    //This array elements will be used to store "the number of being greater."  

    Scanner scan2=new Scanner(System.in);
    System.out.println("Enter the numbers: ");

    for(i=0;i<a;i++)    
        num[i] = scan2.nextInt();

    for (i=0;i<a;i++) {
        for(j=0;j<a;j++) {
            if(num[i]>num[j])   //first time when executes this line, i=0 and j=0 and then i=0;j=1 and so on. each time it finishes second for loop the value of num[i] changes.
                k++;} 
    great[i]=k++;  //At the end of each for loop (second one) k++ contains the total of how many times a number is greater than the others.
    k=0;}  // And then, again k is forced to 0, so that it can collect (the total of how many times a number is greater) for another number.

    System.out.print("Ascending Order: ");
    for(i=0;i<a;i++)
        for(j=0;j<a;j++)
            if(great[j]==i) System.out.print(num[j]+","); //there is a fixed value for each great[j] that is, from 0 upto number of elements(input numbers).
    System.out.print("Discending Order: ");
    for(i=0;i<=a;i++)
        for(j=0;j<a;j++)
            if(great[j]==a-i) System.out.print(+num[j]+",");
}

}

于 2017-11-29T18:05:41.690 回答