查詢陣列中大於其左側相鄰元素的元素?


根據題意,我們有一個包含一些隨機整數值的陣列,我們需要找出大於其左側相鄰元素的數字。

注意 - 使用整型陣列。

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

舉幾個例子

示例1

給定陣列 = [1, 2, -3, -4, 0, 5]。

大於其左側元素的數字 = 2, 0, 5

示例2

給定陣列 = [-1, 0, 4, 6, 8, -5]。

大於其左側元素的數字 = 0, 4, 6, 8

示例3

給定陣列 = [-2, 3, -9, 12, 0, -7]。

大於其左側元素的數字 = 3, 12

演算法

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

步驟2 − 使用for迴圈檢查當前數字是否大於其左側元素。

步驟3 − 如果發現當前數字更大,則列印該數字並進行下一個檢查。

步驟4 − 如果在陣列中沒有找到任何更大的值,則列印“N/A”。

語法

要獲取陣列的長度(陣列中元素的個數),陣列有一個內建屬性,即length

以下是其語法:

array.length

其中,'array' 指的是陣列引用。

多種方法

我們提供了不同的方法來解決這個問題。

  • 使用靜態輸入方法

  • 使用使用者自定義方法

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

方法1:使用靜態輸入方法

在這種方法中,我們宣告一個包含一些隨機整數值的陣列,並使用我們的演算法查詢當前數字是否大於其左側元素,並將該數字作為輸出列印。

示例

public class Main {
   public static void main (String[ ] args){
      
      //declare an integer type array and store some random integer values
      int inputArray[] = { 14, 16, 3, 5, 2};
      
      //declare an integer variable to store the length of the given Array
      int len = inputArray.length;
      int count = 0;
      
      //output line
      System.out.println("Array elements which are greater than its left element:");
      
      //take the loop to find the greater elements
      for (int i = 1; i < len; i++) {
         
         //check whether the present number is greater than its left element or not
         if (inputArray[i] > inputArray[i-1]){
         
         //print the greater elements if available
            System.out.print (inputArray[i] +" ");
            
            //increament the count value
            //if any greater value found
            count+=1;
         }
      }
      
      //if no greater value detected
      if(count==0){
         
         //print not available as output
         System.out.print(" N/A ");
      }
   }
}

輸出

Array elements which are greater than its left element:
16 5

方法2:使用使用者自定義方法

在這種方法中,我們宣告一個包含一些隨機整數值的陣列,並將該陣列和該陣列的長度作為引數傳遞給我們的使用者自定義方法,在使用者自定義方法中,使用該演算法查詢當前數字是否大於其左側元素,並將該數字作為輸出列印。

示例

public class Main {
   public static void main (String[ ] args){
      //declare two arrays 
      int inputArray1[] = {12, 13, 92,-11, 65};
      int inputArray2[] = {5, 4, 3, 2, 1};
      //call the user-defined method 
      greaterThanLeft(inputArray1,inputArray1.length);
      greaterThanLeft(inputArray2,inputArray2.length);
   }
   
   //user defined method
   public static void greaterThanLeft (int[] arr,int n){
      int count = 0;
      System.out.println("Array elements which are greater than its left element:");

      
      //take a for loop to find the greater elements
      for (int i = 1; i < n; i++) {
         
         //check whether the present number is greater than its left element or not
          if (arr[i] > arr[i-1]){
            
            //print the greater elements if available
            System.out.print (arr[i] +" ");
           
           //increament the count value
            //if any greater value found
            count+=1;
         }
      }
      
      //if no greater value detected
      if(count==0){
         System.out.print(" N/A ");
      }
      System.out.print("\n");
   }
}

輸出

Array elements which are greater than its left element:
13 92 65 
Array elements which are greater than its left element:
 N/A

在這篇文章中,我們探討了使用Java程式語言查詢陣列中大於其左側元素的元素的不同方法。

更新於:2023年3月1日

瀏覽量:963

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.