用Java对数组sorting

我试图做一个由10个整数组成的数组,这个数组都有一个随机值,到目前为止这么好。

但是,现在我需要按照从最低到最高的顺序对它们进行sorting,然后将它们打印到屏幕上,我该如何去做呢?

(对不起,有这么多的程序代码很小,我对循环不太好,刚开始使用Java)

public static void main(String args[]) { int [] array = new int[10]; array[0] = ((int)(Math.random()*100+1)); array[1] = ((int)(Math.random()*100+1)); array[2] = ((int)(Math.random()*100+1)); array[3] = ((int)(Math.random()*100+1)); array[4] = ((int)(Math.random()*100+1)); array[5] = ((int)(Math.random()*100+1)); array[6] = ((int)(Math.random()*100+1)); array[7] = ((int)(Math.random()*100+1)); array[8] = ((int)(Math.random()*100+1)); array[9] = ((int)(Math.random()*100+1)); System.out.println(array[0] +" " + array[1] +" " + array[2] +" " + array[3] +" " + array[4] +" " + array[5]+" " + array[6]+" " + array[7]+" " + array[8]+" " + array[9] ); } 

循环也是非常有用的了解,特别是当使用数组,

 int[] array = new int[10]; Random rand = new Random(); for (int i = 0; i < array.length; i++) array[i] = rand.nextInt(100) + 1; Arrays.sort(array); System.out.println(Arrays.toString(array)); // in reverse order for (int i = array.length - 1; i >= 0; i--) System.out.print(array[i] + " "); System.out.println(); 

在println之前添加行并且您的数组被sorting

 Arrays.sort( array ); 

通过实施你自己可以帮助你理解循环。 看泡泡sorting很容易理解:

 public void bubbleSort(int[] array) { boolean swapped = true; int j = 0; int tmp; while (swapped) { swapped = false; j++; for (int i = 0; i < array.length - j; i++) { if (array[i] > array[i + 1]) { tmp = array[i]; array[i] = array[i + 1]; array[i + 1] = tmp; swapped = true; } } } } 

当然,你不应该在生产中使用它,因为对于大型列表,比如由Arrays.sort(array)实现的QuickSortMergeSort

看看Arrays.sort()

我很懒,并添加了循环

 import java.util.Arrays; public class Sort { public static void main(String args[]) { int [] array = new int[10]; for ( int i = 0 ; i < array.length ; i++ ) { array[i] = ((int)(Math.random()*100+1)); } Arrays.sort( array ); for ( int i = 0 ; i < array.length ; i++ ) { System.out.println(array[i]); } } } 

你的数组长度为10.你需要一个variables( i ),它取值从09

 for ( int i = 0 ; i < array.length ; i++ ) ^ ^ ^ | | ------ increment ( i = i + 1 ) | | | +-------------------------- repeat as long i < 10 +------------------------------------------ start value of i Arrays.sort( array ); 

是一个对数组进行sorting的库方法。

 Arrays.sort(yourArray) 

将完美地完成这项工作

看下面,它会给你分类升序和降序两个

 import java.util.Arrays; import java.util.Collections; public class SortTestArray { /** * Example method for sorting an Integer array * in reverse & normal order. */ public void sortIntArrayReverseOrder() { Integer[] arrayToSort = new Integer[] { new Integer(48), new Integer(5), new Integer(89), new Integer(80), new Integer(81), new Integer(23), new Integer(45), new Integer(16), new Integer(2) }; System.out.print("General Order is : "); for (Integer i : arrayToSort) { System.out.print(i.intValue() + " "); } Arrays.sort(arrayToSort); System.out.print("\n\nAscending Order is : "); for (Integer i : arrayToSort) { System.out.print(i.intValue() + " "); } Arrays.sort(arrayToSort, Collections.reverseOrder()); System.out.print("\n\nDescinding Order is : "); for (Integer i : arrayToSort) { System.out.print(i.intValue() + " "); } } /** * @param args the command line arguments */ public static void main(String[] args) { SortTestArray SortTestArray = new SortTestArray(); SortTestArray.sortIntArrayReverseOrder(); }} 

输出将是

 General Order is : 48 5 89 80 81 23 45 16 2 Ascending Order is : 2 5 16 23 45 48 80 81 89 Descinding Order is : 89 81 80 48 45 23 16 5 2 

注意:您可以使用Math.ranodm而不是添加手动数字。 让我知道如果我需要更改代码…

好运…干杯!

这里是如何在你的程序中使用它:

 public static void main(String args[]) { int [] array = new int[10]; array[0] = ((int)(Math.random()*100+1)); array[1] = ((int)(Math.random()*100+1)); array[2] = ((int)(Math.random()*100+1)); array[3] = ((int)(Math.random()*100+1)); array[4] = ((int)(Math.random()*100+1)); array[5] = ((int)(Math.random()*100+1)); array[6] = ((int)(Math.random()*100+1)); array[7] = ((int)(Math.random()*100+1)); array[8] = ((int)(Math.random()*100+1)); array[9] = ((int)(Math.random()*100+1)); Arrays.sort(array); System.out.println(array[0] +" " + array[1] +" " + array[2] +" " + array[3] +" " + array[4] +" " + array[5]+" " + array[6]+" " + array[7]+" " + array[8]+" " + array[9] ); } 

只是FYI,现在可以使用Java 8新API来使用parallelSort对任何types的数组进行sorting

parallelSort使用Java 7中引入的Fork / Join框架将sorting任务分配给线程池中可用的多个线程。

这两个方法可以用来对int数组进行sorting,

 parallelSort(int[] a) parallelSort(int[] a,int fromIndex,int toIndex) 

您可以使用Arrays.sort( array )对int数组进行Arrays.sort( array )

int[] array = {2,3,4,5,3,4,2,34,2,56,98,32,54};

  for (int i = 0; i<array.length; i++) { for (int j = 0; j<array.length; j++) { if (array[i] < array[j]) { int temp = array[i]; array[i] = array[j]; array[j] = temp; } } } 

这是我做的

 System.out.print("Enter number of student: "); input = myScan.nextInt(); int[] scores = new int[input]; String [] students = new String[input]; try{ for(int index = 0; index<input;index++) { System.out.print("Enter student: "); students[index] = myBuff.readLine(); System.out.print("Enter score for "+students[index]+": "); scores[index] = myScan.nextInt(); } //Sorting the numbers for(int index = 0; index<input;index++) { for(int index1 = 0; index1<input-1;index1++) { if(scores[index1]>scores[index1+1]) { contain = scores[index1]; containS = students[index1]; scores[index1] = scores[index1+1]; students[index1] = students[index1+1]; scores[index1+1] = contain; students[index1+1] = containS; } } } 

最有效的方法!

 public static void main(String args[]) { int [] array = new int[10];//creates an array named array to hold 10 int's for(int x: array)//for-each loop! x = ((int)(Math.random()*100+1)); Array.sort(array); for(int x: array) System.out.println(x+" "); } 
 import java.util.*; public class Sample4You { public static void main(String [] args) { int[] YourList = new int[10]; int x; //this is for placing the randomly generated numbers into each index for(x=1; x<10; x++) { YourList[x] = (int)(Math.random()*100); } Arrays.sort(YourList);//we must sort the array "YourList"from least 2 greatest //now we print all the numbers 1 by 1 -- for(x=0; x<YourList.length; x++) { System.out.println("Number in index [" + x + "] = " + YourList[x]); } } } 
  int []array = {5,8,2,1,3,0,1,7}; int min = 0; int temp; for(int i=0; i<array.length; i++) { min = i; for(int j=i; j< array.length; j++) { if(array[j] < array[min]) min = j; } temp = array[i]; array[i] = array[min]; array[min] = temp; } 

对于自然顺序: Array.sort(array)

对于反序: Array.sort(array, Collections.reverseOrder()); – >这是Collections类中的一个静态方法,它将进一步调用自己的内部类来返回一个反向比较器。

您可以使用Arrays.sort()函数。

 sort() method is a java.util.Arrays class method. Declaration : Arrays.sort(arrName) 

优雅的解决scheme是使用streamAPI。它不是在这里使用它的最好方法,因为你花费资源从​​int – > Integer转换,但我想表明,Stream API是强大的工具来创build任何你想要的逻辑的情况。

  int[] ints= {53,95,1,3,534,94,4356,5,1,114}; List<Integer> integers = new ArrayList<>(); Collections.addAll(integers, Arrays.stream(ints).boxed().toArray(Integer[]::new));//Converting int into Integer and put to arrayList integers.sort(Comparator.comparingInt(o -> o));//sorting integers.forEach(System.out::println);//printing 

在性能方面更好地使用类数组。

 Arrays.sort(ints); 

你可以喜欢这个::

 public static void main(String args[]) { int[] array = new int[10]; array[0] = ((int) (Math.random() * 100 + 1)); array[1] = ((int) (Math.random() * 100 + 1)); array[2] = ((int) (Math.random() * 100 + 1)); array[3] = ((int) (Math.random() * 100 + 1)); array[4] = ((int) (Math.random() * 100 + 1)); array[5] = ((int) (Math.random() * 100 + 1)); array[6] = ((int) (Math.random() * 100 + 1)); array[7] = ((int) (Math.random() * 100 + 1)); array[8] = ((int) (Math.random() * 100 + 1)); array[9] = ((int) (Math.random() * 100 + 1)); System.out.println(array[0] + " " + array[1] + " " + array[2] + " " + array[3] + " " + array[4] + " " + array[5] + " " + array[6] + " " + array[7] + " " + array[8] + " " + array[9]); Arrays.sort(array); System.out.println("After sorting array:: "); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } 

你必须导入java.util.Arrays; 进入程序使用Arrays.sort方法。

我正在使用循环来设置数组值

  int[] array = new int[10]; for (int i = 0; i < array.length; i++) { int x = (int) (Math.random() * 100+1); array[i] = x; } boolean flag = true; //flag to exit while loop int tempValue; // temporary value for swapping the array values while (flag) { flag = false; for (int i = 0; i < (array.length) - (1); i++) { if (array[i] > array[i + 1]) { tempValue = array[i]; array[i] = array[i + 1]; array[i + 1] = tempValue; flag = true; } } } System.out.println("Ascending order\n" +Arrays.toString(array)); // print the ordered array values 

它应该在你的代码中这样。

 public static void main(String args[]) { int[] array = new int[10]; array[0] = ((int)(Math.random() * 100 + 1)); array[1] = ((int)(Math.random() * 100 + 1)); array[2] = ((int)(Math.random() * 100 + 1)); array[3] = ((int)(Math.random() * 100 + 1)); array[4] = ((int)(Math.random() * 100 + 1)); array[5] = ((int)(Math.random() * 100 + 1)); array[6] = ((int)(Math.random() * 100 + 1)); array[7] = ((int)(Math.random() * 100 + 1)); array[8] = ((int)(Math.random() * 100 + 1)); array[9] = ((int)(Math.random() * 100 + 1)); Array.sort(array); System.out.println(array[0] + " " + array[1] + " " + array[2] + " " + array[3] + " " + array[4] + " " + array[5] + " " + array[6] + " " + array[7] + " " + array[8] + " " + array[9]); }