在 JAVA 中查詢陣列的中位數元素


在 Java 中,陣列是一個物件。它是一種非基本資料型別,用於儲存相同資料型別的值。

根據問題陳述,我們必須找到存在於陣列中間的元素。

如果陣列包含奇數個元素,則您將獲得一個位於陣列正中間的元素。

如果陣列包含偶數個元素,則您將獲得兩個位於陣列中間的元素。

在本文中,您將瞭解如何使用 Java 程式語言獲取陣列的中位數元素。讓我們探索一下。

為您展示一些例項

例項 1

Suppose the original array is {1, 6, 5, 2, 9, 3, 4, 6}

在執行陣列操作以檢測陣列的中位數元素後,結果將為:

中位數元素為 2 和 9

例項 2

Suppose the original array is {1, 6, 5, 2, 4, 7, 9, 4, 6}

在執行陣列操作以檢測陣列的中位數元素後,結果將為:

中位數元素為 4

例項 3

Suppose the original array is {2, 6, 6, 2, 7, 3, 4, 8, 9}

在執行陣列操作以檢測陣列的中位數元素後,結果將為:

中位數元素為 7

演算法

  • 步驟 1 - 宣告並初始化一個整數陣列。

  • 步驟 2 - 檢查陣列的長度並將其分配給一個 int 變數“nums”。

  • 步驟 3 - 如果 nums 為偶數,則透過應用邏輯列印中間兩個元素。

  • 步驟 4 - 如果 nums 為奇數,則透過應用邏輯列印中間元素。

多種方法

我們以不同的方法提供瞭解決方案。

  • 透過使用陣列元素的靜態初始化

  • 透過使用使用者定義的方法

讓我們逐一檢視程式及其輸出。

方法 1:透過使用陣列元素的靜態初始化

示例

在這種方法中,陣列元素將在程式中初始化。然後根據演算法在 Java 中查詢陣列的中位數元素。

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] nums = new int[]{1, 6, 5, 2, 4, 7, 9, 4, 6};
       
      //logic implementation for middle element
      
      //if even number of array elements are present
      if (nums.length %2 == 0){
         System.out.println("The middle elements are: ");
         
         // even-length array (two middle elements)
         int x = nums[(nums.length/2) - 1];
         System.out.println(x);
         int y = nums[nums.length/2];
         System.out.println(y);
      } 
      
      //if odd number of array elements are present
      else {
      
         // odd-length array (only one middle element)
         int z = nums[nums.length/2];
         System.out.println("The middle elements is: ");
         System.out.println(z);
      }
   }
}

輸出

The middle elements is: 
4

方法 2:透過使用使用者定義的方法

示例

在這種方法中,陣列元素將在程式中初始化。然後透過將陣列作為引數傳遞來呼叫使用者定義的方法,並在方法內部根據演算法在 Java 中查詢陣列的中位數元素。

public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] nums = new int[]{1, 6, 5, 2, 4, 7, 9, 4, 6, 3};
      
      //call the user defined method
      midArray(nums);
   }
   
   //user defined method to find mid element
   public static void midArray(int []nums){
   
      //logic implementation for middle element
      
      //if even number of array elements are present
      if (nums.length %2 == 0){
         System.out.println("The middle elements are: ");
         
         // even-length array (two middle elements)
         int x = nums[(nums.length/2) - 1];
         System.out.println(x);
         int y = nums[nums.length/2];
         System.out.println(y);
      } 
      
      //if odd number of array elements are present
      else{
      
         // odd-length array (only one middle element)
         int z = nums[nums.length/2];
         System.out.println("The middle elements is: ");
         System.out.println(z);
      }
   }
}

輸出

The middle elements are: 
4
7

在本文中,我們探討了如何使用 Java 程式語言查詢陣列的中位數元素。

更新於: 2023 年 1 月 5 日

13K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告