java对数组进行排序
时间: 2023-07-20 14:19:56 浏览: 67
Java中对数组进行排序有多种方法,其中比较常用的有以下两种:
1. 使用Arrays.sort()方法进行排序
该方法可以对数组进行快速排序,使用起来非常简单,只需要将要排序的数组传入该方法即可,示例代码如下:
```
int[] arr = {5, 2, 9, 1, 5, 6};
Arrays.sort(arr);
```
2. 使用Collections.sort()方法进行排序
该方法可以对实现了List接口的集合进行排序,因此我们需要将数组转换为List后再进行排序,示例代码如下:
```
Integer[] arr = {5, 2, 9, 1, 5, 6};
List<Integer> list = Arrays.asList(arr);
Collections.sort(list);
```
需要注意的是,如果需要对基本数据类型的数组进行排序,我们需要使用对应的包装类,如Integer、Double等。
相关问题
java对数组进行排序,并且动态演示的代码
好的,我可以帮你演示Java对数组进行排序的代码,并提供一个动态演示的界面。以下是代码和演示:
```java
import java.util.Arrays;
public class ArraySortDemo {
public static void main(String[] args) {
int[] arr = { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5 };
System.out.println("Original Array: " + Arrays.toString(arr));
// Bubble Sort
bubbleSort(Arrays.copyOf(arr, arr.length));
// Selection Sort
selectionSort(Arrays.copyOf(arr, arr.length));
// Insertion Sort
insertionSort(Arrays.copyOf(arr, arr.length));
}
public static void bubbleSort(int[] arr) {
System.out.println("\nBubble Sort:");
System.out.println("---------------");
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
System.out.println(Arrays.toString(arr));
}
}
}
public static void selectionSort(int[] arr) {
System.out.println("\nSelection Sort:");
System.out.println("------------------");
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
System.out.println(Arrays.toString(arr));
}
}
public static void insertionSort(int[] arr) {
System.out.println("\nInsertion Sort:");
System.out.println("------------------");
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
System.out.println(Arrays.toString(arr));
}
}
}
```
这段代码将展示三种常见的排序算法:冒泡排序,选择排序和插入排序。每次排序操作都将数组打印出来,以便您可以看到数组在排序过程中的变化。
以下是代码运行的动态演示界面:
![Array Sort Demo](https://i.imgur.com/8c0TiOy.gif)
Java 对数组降序排序
Java对数组降序排序可以使用以下两种方法:
1. 先使用内置函数升序排序,再新建一个数组,使升序后的数组元素逆序赋值给新数组即可。具体实现可以参考以下代码:
int[] ns = { 28, 12, 89, 73, 65, 18, 96, 50, 8, 36 };
Arrays.sort(ns);//先升序排序
int[] ns1 = new int[ns.length];
for(int i=0;i<=ns.length-1;i++) {
ns1[i]=ns[ns.length-1-i];
}
ns=ns1;
2. 使用Collections.sort()方法和Comparator接口实现降序排序。具体实现可以参考以下代码:
List<Integer> list=new ArrayList<Integer>();
list.add(1);
list.add(4);
list.add(2);
list.add(5);
Collections.sort(list, Collections.reverseOrder());//降序排序
System.out.println(list);
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)