Java程序以降序排列数组元素
数组是存储在一些连续内存位置的相同数据类型的集合。数组是java.until包中提供预定义排序(静态方式)且无返回值的一个类。以下是Arrays.sort()方法的语法:
public static void sort(int[] ar, int from_index, int to_index)
在上述语法中,我们有
ar - 数组名称的缩写
from_index - 可选参数,排序从该索引开始。
to_index - 可选参数,表示元素的索引。
以下是一个示例
Input :Array = {2, 6, 23, 98, 24, 35, 78}
Output:[98, 78, 35, 24, 23, 6, 2]
Input :Array = {1, 2, 3, 4, 5}
Output:[5, 4, 3, 2, 1]
今天在这篇文章中,我们将学习如何在Java环境中使用列表中存在的数组元素并以降序方式重新排列它们。
以降序排列数组元素的算法:
这里我们编写了可能的算法,我们可以通过它以降序排列数组元素。
步骤 1 - 开始
步骤 2 - 设置 temp = 0。
步骤 3 - 声明一个数组来存放数据。
步骤 4 - 使用arr[] ={5, 2, 8, 7, 1 }初始化数组。
步骤 5 - 打印"原始数组的元素"
步骤 6 - 声明一个临时变量,在交换时存储元素。
步骤 7 - 使用两个for循环。
步骤 8 - 重复i<arr.length。
步骤 9 - 使用第一个for循环来保存元素并遍历所有元素。
步骤 10 - 如果(arr[i]<arr[j]),则
temp = arr[i]
arr[i]=arr[j]
arr[j]=temp
步骤 11 - 使用第二个for循环与剩余元素进行比较
步骤 12 - 打印新行。
步骤 13 - 通过比较和交换对元素进行排序。
步骤 14 - 使用for(i=0;i<arr.length;i++)进行迭代。
步骤 15 - 显示更新后的数组,打印arr[i]。
步骤 16 - 停止
以降序排列数组元素的语法
import java.util.*;
class Tutorialspoint071001 {
public static void main(String[] args){
// Initializing the array for the process
Integer array[] = { 1, 2, 7, 16, 5,6 };
// Sorting the array in a descending order
Arrays.sort(array, Collections.reverseOrder());
// Printing the elements after the process run
System.out.println(Arrays.toString(array));
}
}
遵循的方法
方法 1 - Java程序以降序排列元素
方法 2 - 使用temp函数的Java程序以降序排列元素
方法 3 - 使用通用逻辑的Java程序以降序排列元素
Java程序以降序排列元素
在此Java代码中,我们尝试构建以降序方式对数组元素进行排序过程的逻辑。
示例 1
import java.util.*;
public class tutorialspoint {
public static void main(String[] args){
// Initializing the array for the process run
int array[] = { 1, 2, 3, 7, 5, 16 };
// Sorting the array in ascending order if needed
Arrays.sort(array);
// Reversing the array for the main process
reverse(array);
// Printing the elements from the array
System.out.println(Arrays.toString(array));
}
public static void reverse(int[] array){
// Length of the array is mentioned here
int n = array.length;
// Run the process again. Swapping the first half elements with last half elements
for (int i = 0; i < n / 2; i++) {
// Storing the first half elements in a temp manner
int temp = array[i];
// Assigning the first half to the last half to get result
array[i] = array[n - i - 1];
// Assigning the last half to the first half to get the result
array[n - i - 1] = temp;
}
}
}
输出
[16, 7, 5, 3, 2, 1]
使用temp函数的Java程序以降序排列元素
在此Java代码中,我们可以构建一个逻辑,使用temp函数以降序方式对数组中存在的元素进行排序。
示例 2
import java.util.Scanner;
public class Descendingtutorialspountrddarb{
public static void main(String[] args) {
int n, temp;
Scanner s = new Scanner(System.in);
System.out.print("Enter no. number of elements you want in the array---->:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements here now to run the code further ----> :");
for (int i = 0; i < n; i++) {
a[i] = s.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Descending Order Output Is Here. Have A Look!:");
for (int i = 0; i < n - 1; i++) {
System.out.print(a[i] + ",");
}
System.out.print(a[n - 1]);
}
}
输出
Enter no. number of elements you want in the array---->:7 Enter all the elements here now to run the code further ----> : 1 2 3 16 4 5 6 Descending Order Output Is Here. Have A Look!:16,6,5,4,3,2,1
使用通用逻辑对元素进行降序排序
在此Java代码中,我们编写了一个逻辑,使用一些通用函数以降序方式对数组中存在的元素进行排序。
示例 3
public class Tutorialspoint {
public static void main(String[] args) {
//Initialize array for the process
int [] arr = new int [] {16, 2022, 2001, 1997, 7};
int temp = 0;
//Displaying elements of an original array to go further
System.out.println("Elements of original array are ---->: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
//Sort the array in descending order. Please go further
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] < arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
System.out.println();
//Displaying elements of array after sorting process done.
System.out.println("Elements of array sorted in descending order is here ---->: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
输出
Elements of original array are ---->: 16 2022 2001 1997 7 Elements of array sorted in descending order is here ---->: 2022 2001 1997 16 7
结论
我们详细了解了数组元素排序问题。今天,我们使用上述语法和算法中的各种排序方法来解决此问题。希望通过本文,您已经对如何在Java环境中使用降序方式对数组元素进行排序有了广泛的了解。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
Javascript
PHP