简体   繁体   English

如何从输入中对 Arrays 进行排序

[英]How to Sort an Arrays from inputs

I want fix this code?我要修复此代码? It's just given me only first 2 numbers: And I want to sort the arrays numbers that input from the user!它只给了我前两个数字:我想对用户输入的 arrays 数字进行排序! How Autcally Can I do that?我怎么能做到这一点? An Example:一个例子:

Input Format输入格式

Prompt user to insert his array提示用户插入他的数组

Sample Output样品 Output

Enter your array: [8,5,1,33,6,11,48,95,648,44]输入您的数组:[8,5,1,33,6,11,48,95,648,44]

Your ascending ordered array: [1,5,6,8,11,33,44,48,648,44]您的升序数组:[1,5,6,8,11,33,44,48,648,44]

How CAN I fix my code that can given me such this output?如何修复可以给我这样的 output 的代码?

int user,i;
   Scanner scan = new Scanner(System.in);
   System.out.println("Enter your array: "); 
   user = scan.nextInt();
   int [] array = new int[user];
   for(i = 0; i < array.length; i++) {
       array[i] = scan.nextInt();
       System.out.print(array[i] + " ");
    }
    
}

Utilizar Clase Arrays实用程序类 Arrays

import java.util.Arrays;

public class SimpleTesting {
    public static void main(String[] args) {    
        int[] arr = new int[]{12,3,5,21,4,85,6,9,2,1};
        for (int i : arr) {
            System.out.print(i+" ");
        }
        arr = Arrays.stream(arr).sorted().toArray();
        System.out.println("\nAfter Sorting...");
        for (int i : arr) {
            System.out.print(i+" ");
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM