如何在 Java 中对数组元素进行排序
本文介绍了如何在 Java 中对数组元素进行排序,并列举了一些示例代码来理解它。
数组元素排序有几种方法,如 Arrays.sort()
、sorted()
、parallelSort()
等方法。我们来看看例子。
使用 Java 中的 sort()
方法对数组元素进行排序
这里,我们使用 Arrays
类的 sort()
方法对元素进行排序。这个方法按照升序对元素进行排序。请看下面的例子。
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+" ");
}
Arrays.sort(arr);
System.out.println("\nAfter Sorting...");
for (int i : arr) {
System.out.print(i+" ");
}
}
}
输出:
12 3 5 21 4 85 6 9 2 1
After Sorting...
1 2 3 4 5 6 9 12 21 85
在 Java 中对数组元素进行排序
如果我们不想使用任何 Java 内置的方法,那么使用这段代码将数组元素按升序排序。
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+" ");
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (arr[i] < arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println("\nAfter Sorting...");
for (int i : arr) {
System.out.print(i+" ");
}
}
}
输出:
12 3 5 21 4 85 6 9 2 1
After Sorting...
1 2 3 4 5 6 9 12 21 85
在 Java 中使用 parallelSort()
方法对一个数组元素进行排序
如果你使用的是 Java 8 或更高版本,你可以使用 Arrays 类的 parallelSort()
方法。这个方法对于在多线程环境下的排序很有用。请看下面的例子。
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+" ");
}
Arrays.parallelSort(arr);
System.out.println("\nAfter Sorting...");
for (int i : arr) {
System.out.print(i+" ");
}
}
}
输出:
12 3 5 21 4 85 6 9 2 1
After Sorting...
1 2 3 4 5 6 9 12 21 85
在 Java 中使用 parallelSort()
方法对数组元素进行排序
Java 提供了一个重载的 parallelSort()
方法来排序子数组。这意味着我们可以将一个数组中的一些元素从一个特定的索引排序到另一个特定的索引(结束索引)。这个方法需要三个参数,一个是数组,第二个和第三个是要排序的数组的开始和结束索引。请看下面的例子。
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+" ");
}
Arrays.parallelSort(arr, 0, 5);
System.out.println("\nAfter Sorting...");
for (int i : arr) {
System.out.print(i+" ");
}
}
}
输出:
12 3 5 21 4 85 6 9 2 1
After Sorting...
3 4 5 12 21 85 6 9 2 1
使用 Java 中的 parallelSort()
方法对一个数组元素进行排序
如果你想使用 Java 的流功能,那么就使用 sorted()
方法将元素排序,通过使用 toArray()
,我们将元素收集到一个数组中。
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+" ");
}
}
}
输出:
12 3 5 21 4 85 6 9 2 1
After Sorting...
1 2 3 4 5 6 9 12 21 85
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
如何从 Pandas 的日期时间列中提取月份和年份
发布时间:2024/04/23 浏览次数:160 分类:Python
-
我们可以分别使用 dt.year()和 dt.month()方法从 Datetime 列中提取出年和蛾。我们还可以使用 pandas.DatetimeIndex.month 以及 pandas.DatetimeIndex.year 和 strftime()方法提取年份和月份。
如何检查 NaN 是否存在于 Pandas DataFrame 中
发布时间:2024/04/23 浏览次数:208 分类:Python
-
我们可以使用 isnull()和 isna()方法检查 Pandas DataFrame 中是否存在 NaN。
如何在 Pandas DataFrame 的列中将所有 NaN 值替换为零
发布时间:2024/04/23 浏览次数:198 分类:Python
-
在 Pandas 库中使用 df.fillna(),df.replace()方法在 DataFrame 中将 NaN 值替换为零
如何在 Pandas 中更改列的数据类型
发布时间:2024/04/23 浏览次数:183 分类:Python
-
本教程介绍了如何通过使用 to_numaric,as_type 和 infer 对象来更改 Pandas 中列的数据类型。
如何对 Pandas 中的 DataFrame 行随机排序
发布时间:2024/04/23 浏览次数:128 分类:Python
-
我们可以使用 sample(),shuffle()和 permutation()方法随机地对 Pandas 中的 DataFrame 行进行随机排序。
如何获取 Pandas DataFrame 的行数
发布时间:2024/04/23 浏览次数:71 分类:Python
-
本教程介绍如何通过使用 shape,len()来获取 Pandas DataFrame 的行数,以及有多少行元素满足条件。