在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 次浏览

启动您的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.