Java 找到小于给定数字的数组元素数目

Java 找到小于给定数字的数组元素数目

数组是一种线性数据结构,其中元素存储在连续的内存位置中。

根据问题描述,找到小于给定数字的元素数目意味着我们只需要比较和计数数组中较小的元素。

让我们来探索本文,看看如何使用Java编程语言来实现。

举例说明

实例-1

假设我们有以下数组:

[10, 2, 3, -5, 99, 12, 0, -1]
and the number is 9
Now the number of elements that are smaller than 9 are
[2, 3, -5,0, -1] = 5 elements

实例-2

假设我们有以下数组

[55, 10, 29, 74, 12, 45, 6, 5, 269]
and the number is 50
Now the number of elements that are smaller than 50 are
[10, 29, 12, 45, 6, 5] = 6

实例-3

假设我们有以下数组

[556, 10, 259, 874, 123, 453, -96, -54, -2369]
and the number is 0
Now the number of elements that are smaller than 0 are
[-96, -54, -2369] = 3

步骤

步骤-1

  • 步骤1 - 存储数组元素

  • 步骤2 - 使用for循环遍历所有数组元素。

  • 步骤3 - 将所有元素与给定数字进行比较

  • 步骤4 - 使用计数器计算所有小于该数字的元素并打印计数。

步骤-2

  • 步骤1 - 存储数组元素

  • 步骤2 - 对数组进行排序。

  • 步骤3 - 比较并找到大于给定数字的元素的索引

  • 步骤4 - 要找到小于给定数字的元素的数量,我们打印我们得到的索引。

语法

为了获取一个数组的长度(即数组中的元素数量),有一个内置的数组属性,即 length(长度)。

以下是其语法参考 −

array.length

其中,’array’ 是指数组的引用。

您可以使用Arrays.sort()方法对数组按升序进行排序。

Arrays.sort(array_name);

多种方法

我们以不同的方法提供了解决方案。

  • 不使用排序

  • 使用排序

让我们一一看看程序及其输出。

方法1:不使用排序

在这个方法中,我们使用一个循环将所有元素与该数字进行比较,只计算较小的元素。

示例

public class Main {
   public static void main(String[] args) {

      // The array elements
      int arr[] = { 556, 10, 259, 874, 123, 453, -96, -54, -2369}, num = 0;
      System.out.println("The array elements are-");

      // Print the array elements
      for (int i : arr) {
         System.out.print(i + ", ");
      }

      // The counter two count all elements smaller than the number
      int count = 0;

      // Count all elements smaller than num
      for (int i = 0; i < arr.length; i++) {
         if (arr[i] < num) {
            count++;
         }
      }
      System.out.println("\nThe number of array elements that are smaller than " + num + " are " + count);
   }
}

输出

The array elements are-
556, 10, 259, 874, 123, 453, -96, -54, -2369, 
The number of array elements that are smaller than 0 are 3

方法2:使用排序

在这种方法中,我们使用Arrays.sort()方法对数组进行排序,然后找到第一次出现元素大于该数字的索引。该索引是小于该数字的元素数量。

示例

import java.util.Arrays;
public class Main{
   public static void main(String[] args) {

      // The array elements
      int arr[] = { 556, 10, 259, 874, 123, 453, -96, -54, -2369}, num = 20;
      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);

      // Find the index of the first element in the array greater than the given number 
      int index = 0;
      for (int i = 0; i < arr.length; i++) {
         if (arr[i] > num) {
            index = i;
            break;
         }
      }

      // To find the number of elements smaller than
      // the number we print the index we onbtained
      System.out.println("\nThe number of array elements that are lesser than " + num + " are " + (index));
   }
}

输出

The array elements are-
556, 10, 259, 874, 123, 453, -96, -54, -2369, 
The number of array elements that are lesser than 20 are 4

在本文中,我们探讨了如何通过使用Java编程语言来找出数组中小于给定数字的元素数量。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程