Java 找出大于其左边元素的数组元素

Java 找出大于其左边元素的数组元素

根据问题描述,我们有一个带有一些随机整数值的数组,我们需要找出大于其左边元素的数字。

注意-要使用整数数组。

让我们深入探讨一下这篇文章,了解如何使用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编程语言探索了不同的方法来找出数组元素大于其左侧元素的元素。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程