在 Java 中查找数组中第一大和第二大的元素之间的差值
在 Java 中,数组是一个对象。它是一种非基本数据类型,用于存储相同数据类型的多个值。
根据问题陈述,我们需要找到数组中最大和第二大数字之间的差值。
注意 - 数组必须是整数数组。
让我们探索本文,看看如何使用 Java 编程语言来实现它。
为您展示一些实例
实例 1
Suppose the original array is {22, 45, 1, 10, 52, 27}
执行操作后,结果将为 -
数组的第二大元素为 - 45
数组的最大元素为 - 52
数组中最大和第二大元素的差值为 - 7
实例 2
Suppose the original array is {5, 41, 11, 21, 32, 27}
执行操作后,结果将为 -
数组的第二大元素为 - 32
数组的最大元素为 - 41
数组中最大和第二大元素的差值为 - 9
实例 3
Suppose the original array is {18, 25, 61, 19, 55, 29}
执行操作后,结果将为 -
数组的第二大元素为 - 55
数组的最大元素为 - 61
数组中最大和第二大元素的差值为 - 6
算法
步骤 1 - 声明并初始化一个整数数组。
步骤 2 - 将数组按升序排序。
步骤 3 - 使用“arr[n - 2]”和“arr[n - 1]”获取数组的第二大和最大数字。
步骤 4 - 减去最大和第二大元素。
步骤 5 - 打印数字。
语法
要获取数组的长度(数组中的元素数量),数组有一个内置属性,即length。
下面是它的语法 -
array.length
其中,“array”指的是数组引用。
要对数组进行排序,java.util 包的 Arrays 类提供了一个内置的 sort() 方法。
下面是它的语法 -
Arrays.sort(arr);
默认情况下,它会按升序对数组进行排序。
其中,“arr”指的是数组引用。
多种方法
我们提供了不同方法的解决方案。
使用数组的静态初始化
使用用户定义的方法
让我们逐一查看程序及其输出。
方法 1:使用数组的静态初始化
示例
在这种方法中,数组元素将在程序中初始化。然后根据算法找到数组中最大和第二大数字之间的差值。
import java.util.*;
public class Main {
//main method
public static void main(String[] args) {
//Declare and initialize the array elements
int arr[] = {5, 41, 11, 21, 32, 27};
//get length of an array
int n = arr.length;
//sorting an array in ascending order
Arrays.sort(arr);
//getting the second largest number of an array
int p = arr[n - 2];
//print the second largest number of an array
System.out.println("The second largest element of an array is: " + p);
//sorting an array in ascending order
Arrays.sort(arr);
//getting the largest number of an array
int q = arr[n - 1];
//print the largest number of an array
System.out.println("The largest element of an array is: " + q);
//finding the difference between largest and second largest number
int r = q - p;
System.out.println("The difference of largest and second largest element of an array is: " + r);
}
}
输出
The second largest element of an array is: 32 The largest element of an array is: 41 The difference of largest and second largest element of an array is: 9
方法 2:使用用户定义的方法
示例
在这种方法中,数组元素将在程序中初始化。然后通过将数组作为参数调用用户定义的方法,并在方法内部根据算法找到数组中最大和第二大数字之间的差值。
import java.util.*;
public class Main{
//main method
public static void main(String[] args) {
//Declare and initialize the array elements
int arr[] = {18, 25, 61, 19, 55, 29};
//calling user defined method
diff(arr);
}
//user defined method
static void diff(int []arr){
//get length of an array
int n = arr.length;
//sorting an array in ascending order
Arrays.sort(arr);
//getting the second largest number of an array
int p = arr[n - 2];
//print the second largest number of an array
System.out.println("The second largest element of an array is: " + p);
//sorting an array in ascending order
Arrays.sort(arr);
//getting the largest number of an array
int q = arr[n - 1];
//print the largest number of an array
System.out.println("The largest element of an array is: " + q);
//finding the difference between largest and second largest number
int r = q - p;
System.out.println("The difference of largest and second largest element of an array is: " + r);
}
}
输出
The second largest element of an array is: 55 The largest element of an array is: 61 The difference of largest and second largest element of an array is: 6
在本文中,我们探讨了如何使用 Java 编程语言找到数组中最大和第二大数字之间的差值。
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP