在Java中查詢至少包含一個較小元素的陣列元素


陣列是一種線性資料結構,其中元素儲存在連續的記憶體位置。

根據問題陳述,我們需要列印所有至少包含一個較小元素的元素。簡單來說,我們可以說除了最小值以外列印陣列的所有元素,因為最小值沒有比它更小的元素。

讓我們探索這篇文章,看看如何使用Java程式語言來實現它。

展示一些示例

示例1

Suppose we have the below array
[10, 2, 3, -5, 99, 12, 0, -1]
Now all elements that have at least one smaller element are = 10, 2, 3, 99, 12, 0, -1

示例2

Suppose we have the below array
[55, 10, 29, 74, 12, 45, 6, 5, 269]
Now all elements that have at least one smaller element are = 55, 10, 29, 74, 12, 45, 6, 269

示例3

Suppose we have the below array
[556, 10, 259, 874, 123, 453, -96, -54, -2369]
Now all elements that have at least one smaller element are = 556, 10, 259, 874, 123, 453, -96, -54

演算法

演算法1

  • 步驟1 - 儲存陣列元素。

  • 步驟2 - 使用for迴圈遍歷所有陣列元素。

  • 步驟3 - 比較所有元素以找到最小值。

  • 步驟4 - 使用foreach迴圈並列印除最小值以外的所有元素。

演算法2

  • 步驟1 - 儲存陣列元素。

  • 步驟2 - 按升序對陣列進行排序。

  • 步驟3 - 執行for迴圈,從第二個元素遍歷到末尾,並列印所有元素。

語法

要獲取陣列的長度(陣列中的元素個數),陣列有一個內建屬性,即length -

以下是它的語法:

array.length

其中“array”指的是陣列引用。

您可以使用Arrays.sort()方法按升序對陣列進行排序。

Arrays.sort(array_name);

多種方法

我們提供了不同的方法來解決這個問題。

  • 不使用排序

  • 使用排序

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

方法1:不使用排序

在這種方法中,我們使用for迴圈查詢最小元素,然後列印除最小元素以外的所有元素。

示例

public class Main {
   public static void main(String[] args) {
      
      // The array elements
      int arr[] = { 10, 2, 3, 99, 12, 10 };
      System.out.println("The array elements are-");
      
      // Print the array elements
      for (int i : arr) {
         System.out.print(i + ", ");
      }
      
      // Initialize the first element as smallest and compare
      int smallest = arr[0];
      
      // Find the smallest element in the array
      for (int i = 0; i < arr.length; i++)
         if (arr[i] < smallest)
            smallest = arr[i];
      
      // Print array elements that have at least one smaller element
      System.out.println("\nThe array elements that have at least one smaller element are-");
      for (int i : arr) {
         if (i != smallest)
            System.out.print(i + ", ");
      }
   }
}

輸出

The array elements are-
10, 2, 3, 99, 12, 10, 
The array elements that have at least one smaller element are-
10, 3, 99, 12, 10,

方法2:使用排序

在這種方法中,我們使用Arrays.sort()方法對陣列進行排序,然後列印除第一個元素以外的所有元素。

示例

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
   
      // The array elements
      int arr[] = { 10, 2, 3, 99, 12, 10 };
      System.out.println("The array elements are-");
      
      // Print the array elements
      for (int i : arr) {
         System.out.print(i + ", ");
      }
      
      // Sort the array
      Arrays.sort(arr);
      
      // Print the array elements from the 2nd element
      System.out.println("\nThe array elements that have at least one smallerelement are-");
      for (int i = 1; i < arr.length; i++) {
         System.out.print(arr[i] + ", ");
      }
   }
}

輸出

The array elements are-
10, 2, 3, 99, 12, 10, 
The array elements that have at least one smallerelement are-
3, 10, 10, 12, 99, 

在這篇文章中,我們探討了如何使用Java程式語言查詢至少包含一個較小元素的所有陣列元素。

更新於:2023年1月31日

171 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告