在 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 中查詢陣列中每個元素的頻率。
廣告
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP