在 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 程式語言找到陣列中最大和第二大數字之間的差值。

更新於: 2023 年 1 月 5 日

2K+ 瀏覽量

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告