如何在 Java 中查詢一位陣列元素?


在一個給定的包含一些隨機整數值的陣列中,我們必須找出給定陣列中存在的一位數字,並將這些一位數字作為輸出打印出來。

陣列可以包含正數或負數,無論數字中包含多少位數字。因為這裡所有元素都屬於數字型別。

注意 - 獲取一個正整數陣列。

讓我們深入研究本文,瞭解如何使用 Java 程式語言來實現它。

向您展示一些例項

例項-1

給定陣列 = [1, 12, 3, 4, 10, 15]。

給定陣列中存在的一位數字元素 = 1, 3, 4

例項-2

給定陣列 = [451, 102, 3, 14, 100, 15]。

給定陣列中存在的一位數字元素 = 3

例項-3

給定陣列 = [111, 612, 83, 4, 10, 5, 9, 89]。

給定陣列中存在的一位數字元素 = 4, 5, 9

演算法

演算法-1

步驟 1 - 透過靜態輸入方法宣告一個包含一些隨機整數值的陣列。

步驟 2 - 使用 for 迴圈,我們檢查一個條件,即數字是否在 0 到 9 之間。

步驟 3 - 如果條件滿足,我們確認該數字是一位數字。

步驟 4 - 將這些一位數字作為輸出打印出來。

演算法-2

步驟 1 - 透過靜態輸入方法宣告一個包含一些隨機整數值的陣列。

步驟 2 - 初始化一個迴圈,我們檢查一個條件,即該數字對 10 取模的值是否等於該數字本身。這表明該數字是一位數字。

步驟 3 - 如果條件滿足,我們確認該數字是一位數字。

步驟 4 - 將這些一位數字作為輸出打印出來。

多種方法

我們提供了不同方法的解決方案。

  • 使用簡單的數字檢查方法

  • 使用模數檢查方法

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

方法-1:使用簡單的數字檢查方法

在這種方法中,我們宣告一個包含一些隨機整數值的陣列,並使用我們的演算法找到一位數字,並將這些數字作為輸出打印出來。

示例

public class Main {
   public static void main (String[] args) {
     
      //declare an integer type array 
      //and store some random positive integer values
      int inputArray[] = {7, 12, 5, 9, 15};
      
      //declare an integer value for counting single digit elements
      //and initialize it with 0
      int count = 0;
      System.out.print ("Single digit elements present in array are: ");
      
      //initiate the loop to find the single digits
      for (int i = 0; i < inputArray.length; i++)  {
        
         //take a for loop to check every values
         if (inputArray[i] >= 0 && inputArray[i] <= 9){
           
            //If the number satisfied above condition
            //its a single digit number so print that number
            System.out.print(inputArray [i] + " ");
            
            //increment the count value if any single digit found
            count+=1;
         }
      }
      
      //if no single digit detected
      if(count==0){
     
        //print not available as output
         System.out.print(" N/A ");
      }
   }
}

輸出

Single digit elements present in array are: 7 5 9

方法-2:使用模數檢查方法

在這種方法中,我們宣告一個包含一些隨機整數值的陣列,並使用我們的演算法找到一位數字,並將這些數字作為輸出打印出來。

示例

public class Main {
   public static void main (String[] args) {
      
      //declare an integer arrays and store some random +ve integer values
      int inputArray1[] = {20, 12, 33, 2, 11, 3};

      //declare an integer value for counting single digit
      //initialize it with value 0
      int count = 0;
      System.out.print ("Single digit elements present in the array are: ");

      //take a for loop to find the single digits in first array
      for (int i = 0; i < inputArray1.length; i++) {
        
         //in each loop find the modulus value with 10
         //so that we can find the single digit value
         if (inputArray1[i] % 10 == inputArray1[i]){
            //If the number satisfied above condition its a single digit number
            System.out.print(inputArray1 [i] + " ");
             
            //increment the count value if any single digit found
            count+=1;
         }
      }

      //if no single digit detected
      if(count==0){
         //print not available as output
         System.out.print(" N/A ");
      }
   }
}

輸出

Single digit elements present in the array are: 2 3

在本文中,我們探討了使用 Java 程式語言查詢陣列中一位數字元素的不同方法。

更新於:2023 年 3 月 9 日

2K+ 次檢視

啟動您的 職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.