Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按升序排序整数-Java_Java_Arrays_Sorting - Fatal编程技术网

按升序排序整数-Java

按升序排序整数-Java,java,arrays,sorting,Java,Arrays,Sorting,我需要帮助理解如何对数字进行排序。 下面是我到目前为止想到的,但它不起作用。你能指出错误并告诉我怎么办吗 我看到你们中的一些人在使用java.util.array。你能给我描述一下它的功能吗 import static java.lang.System.*; import java.util.*; public class Lab07v2_Task10{ public static void main (String[] args){ Scanner o

我需要帮助理解如何对数字进行排序。 下面是我到目前为止想到的,但它不起作用。你能指出错误并告诉我怎么办吗

我看到你们中的一些人在使用
java.util.array
。你能给我描述一下它的功能吗

import static java.lang.System.*;
import java.util.*;

    public class Lab07v2_Task10{
    public static void main (String[] args){
            Scanner orcho = new Scanner (in);
            int quantity = 5;
            int[] myArray = new int [quantity];
            out.println("Please enter 5 numbers");

            for(int count = 0; count<myArray.length; count++){
                myArray[count] = orcho.nextInt();
            }

            int maxSoFar = myArray[0];

            for(int count = myArray.length-1; count>=0; count--){
                if(myArray[count] > maxSoFar){
                maxSoFar = myArray[count];
            }
            out.println(maxSoFar);
       }
    }
}   
导入静态java.lang.System.*;
导入java.util.*;
公共类Lab07v2_任务10{
公共静态void main(字符串[]args){
扫描仪orcho=新扫描仪(英寸);
整数=5;
int[]myArray=新int[数量];
out.println(“请输入5个数字”);
对于(int count=0;count=0;count--){
if(myArray[count]>maxSoFar){
maxSoFar=myArray[count];
}
out.println(maxSoFar);
}
}
}   

java.util.array.sort(int[])方法将
int
的指定数组按升序进行排序

试试这个

// sorting array
  java.util.Arrays.sort(myArray);

   // let us print all the elements available in list
   System.out.println("The sorted int array is:");
   for (int number : myArray) {
   System.out.println("Number = " + number);
   }
   }
Arrays.sort
是一种在
java.util
包中提供的实用方法

其中,
Arrays
是一个系统定义的实用程序类,它包含方法
sort(int[])
int[]
(array)作为参数,并在对该数组排序后重新分配数组

欲了解更多详细信息,或无解决方案。 这个想法是采取几个步骤,做一个for循环。假设你在中间。第一部分已排序,其余部分将完成

然后根据已经排序的内容处理当前元素

int maxSoFar = myArray[0];
for (int i = 1; i < myArray.length; i++) {
    // The array 0, ..., i-1 is sorted
    if (myArray[i] >= maxSoFar) {
        // Still sorted
        maxSoFar = myArray[i];
    } else {
        // myArray[i] must be shifted left
        ...
    }
    // Now the array 0, ..., i is sorted
}
int-maxSoFar=myArray[0];
for(int i=1;i=maxSoFar){
//仍然分类
maxSoFar=myArray[i];
}否则{
//myArray[i]必须左移
...
}
//现在数组0,…,i被排序
}

这是一个常见的技巧:假设部分已经完成,处理一小步,然后继续。

您的程序现在运行的方式:它将打印5个数字,它打印的数字是在该迭代中找到的最高数字

您希望它工作的方式:从最低到最高排序5个数字。然后打印这5个数字。这是程序中气泡排序的实现:

for(int i = 0; i< myArray.length; i++){
        for(int j = 0; j < myArray.length-1; j++){
            if(myArray[j] > myArray[j+1]){ //if the current number is less than the one next to it
                int temp = myArray[j]; //save the current number 
                myArray[j] = myArray[j+1]; //put the one next to it in its spot
                myArray[j+1] = temp; //put the current number in the next spot
            }
        }
    }
for(int i=0;imyArray[j+1]){//如果当前数字小于它旁边的数字
int temp=myArray[j];//保存当前编号
myArray[j]=myArray[j+1];//把它旁边的那个放在它的位置上
myArray[j+1]=temp;//将当前编号放在下一个位置
}
}
}
这可能是最容易理解的一种。基本上,对于数组长度的任意倍,对数字进行梳理,并将下一个最高的数字尽可能向上移动


排序完成后,您可以打印数字。

您可以只使用
数组。sort
..@Mena这有点离题,您应该能够“手动”排序数字,即使您使用
数组。sort
@Maljam这就是为什么它是一个注释,而不是答案。只是想让OP的生活更轻松。众所周知,
数组
集合
方法是高度优化的。实现自己的算法可能毫无意义,即使在学习时也是如此。您可以直接从源代码中复制。出于学习目的,您应该首先阅读,但是根据数组的大小,您可以自己找到一个简单的算法……因为这显然是编程练习,我认为使用数组不会给您带来太多的教学。您可以尝试实现一个简单的冒泡排序,如下所述:谢谢!使用临时工有帮助!