在 Java 中查找数组中正数、负数和零元素的个数


在 Java 中,数组是一种非基本数据类型,它存储相同数据类型的多个值。

根据题意,我们需要查找每个元素的频率,即每个元素在数组中出现的次数。

让我们看看如何使用 Java 编程语言来实现。

展示一些示例

示例 1

Suppose the original array is {21, 10, 26, 21, 10, 33, 33, 20, 10, 21}

找到数组中每个元素的频率后,结果将是:

Element | Frequency
------------------------
21 | 3
10 | 3
26 | 1
33 | 2
20 | 1

示例 2

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

找到数组中每个元素的频率后,结果将是:

Element | Frequency
-----------------------
2 | 3
3 | 3
9 | 2
5 | 1
1 | 1

算法

  • 步骤 1 - 声明并初始化一个整数数组。

  • 步骤 2 - 遍历数组元素,使用 for 循环计数频率。

  • 步骤 3 - 如果已经处理过则跳过元素,使用另一个 for 循环计数频率。

  • 步骤 4 - 打印所需结果。

语法

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

以下是其语法:

array.length

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

多种方法

我们提供了不同的方法来解决这个问题。

  • 使用数组元素的静态初始化

  • 使用用户自定义方法

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

方法 1:使用数组元素的静态初始化

示例

在这种方法中,数组元素将在程序中初始化。然后,根据算法遍历数组元素,使用 for 循环计数频率,并使用另一个 for 循环计数频率。

import java.util.Arrays;
public class Main{

   //main method
   public static void main(String args[]){
   
      //Declare and initialize the array elements
      int arr[] = new int[]{ 21, 10, 26, 21, 10, 33, 33, 20, 10, 21 };
      int n = arr.length;
      boolean vis[] = new boolean[n];
      Arrays.fill(vis, false);

      System.out.println("---------------------------------------");  
      System.out.println(" Element | Frequency");  
      System.out.println("---------------------------------------");
      // Traverse through array elements and count frequencies
      for (int i = 0; i < n; i++){
      
         // Skip this element if already processed
         if (vis[i] == true)
            continue;

         //to Count frequency
         int count = 1;
         for (int j = i + 1; j < n; j++){
            if (arr[i] == arr[j]){
               vis[j] = true;
               count++;
            }
         }
         System.out.println(arr[i] + " | " + count);
      }
      System.out.println("----------------------------------------");  
   }
}

输出

---------------------------------------
 Element | Frequency
---------------------------------------
21 | 3
10 | 3
26 | 1
33 | 2
20 | 1
----------------------------------------

方法 2:使用用户自定义方法

示例

在这种方法中,数组元素将在程序中初始化。然后,通过将数组作为参数传递来调用用户自定义方法,并在方法内部根据算法遍历数组元素,使用 for 循环计数频率,并使用另一个 for 循环计数频率。

import java.util.Arrays;
public class Main{

   //user defined method
   public static void frequency(int arr[], int n){
      boolean vis[] = new boolean[n];
      Arrays.fill(vis, false);

      System.out.println("---------------------------------------");  
      System.out.println(" Element | Frequency");  
      System.out.println("---------------------------------------");
      // Traverse through array elements and count frequencies
      for (int i = 0; i < n; i++) {

         // Skip this element if already processed
         if (vis[i] == true)
            continue;

         //to Count frequency
         int count = 1;
         for (int j = i + 1; j < n; j++) {
            if (arr[i] == arr[j]) {
               vis[j] = true;
               count++;
            }
         }
         System.out.println(arr[i] + " | " + count);
      }
      System.out.println("----------------------------------------");  
   }


   //main method
   public static void main(String args[]){
   
      //Declare and initialize the array elements
      int arr[] = new int[]{ 88, 22, 88, 22, 15, 32, 22, 32, 89};
      int number = arr.length;
      
      //call a user defined method
      frequency(arr, number);
   }
}

输出

---------------------------------------
 Element | Frequency
---------------------------------------
88 | 2
22 | 3
15 | 1
32 | 2
89 | 1
----------------------------------------

在这篇文章中,我们探讨了如何在 Java 中查找数组中每个元素的频率。

更新于:2023年1月5日

422 次浏览

启动您的 职业生涯

完成课程获得认证

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