首页 > Java > java教程 > 正文

Java程序返回列表中的最大元素

PHPz
发布: 2023-08-19 17:17:07
转载
1136 人浏览过

Java程序返回列表中的最大元素

我们可以使用数组循环来从列表中返回最大的元素。主要是通过比较模型来实现的。在某个列表中,最大的数字将与该列表中的所有元素进行比较。该过程将考虑“n”作为输入数量,并将其作为数据值存储在数组中。之后,程序将在循环结束后在输出控制台上显示最大的元素。

在本文中,我们将帮助您理解并编写一些Java代码,通过这些代码您可以从数组列表中找到最大的元素。

如何使用Java从数组中选择最大的数字?

We can find a largest number by sorting an array. To define a void ArrayList and add all elements of array to it. Passing the ArrayList to Collections.max() and the entire process will take a run.

  • For this operation, you can declare a set of input as a form of array at the beginning. This creates a base to execute a logic. The algorithm uses this loop to find out the particular result (largest number of that loop).

Example

的中文翻译为:

示例

Let's take an example.

arr[]= {1,10,4,15,9,85,63,108}
登录后复制

输出

Output: 108
登录后复制
  • 从数组中找到最大的数,通常会使用两种类型的函数 -

    • Max () – Use to find the max function from the list

    • for Loop - Use to perform iteration for every element.

  • 首先,您应该声明一个数组,然后对其进行初始化。对于迭代,我们需要两个循环,然后比较元素以获取最大的数字,数据需要按降序进行交换。

在列表中找到最大元素的算法

Here is the general algorithm for to find out the largest element in a list by using Java −

  • 第一步 − 开始

  • 第2步 − 初始化arr[]

  • 第三步 − max=arr[0]

  • 第4步 − i=0;i

  • 第四步 - 如果 (arr[i]>max)max=arr[i]

  • 步骤 5(1) − 打印

  • 步骤 5(2) − 打印 MAX

  • Step 6 − Terminate

Syntax

有两种方法可以执行该操作。在下面的语法中描述了这两种方法。

  • coll means; the total collection from which the maximum element will be filtered out.

  • comp意味着; 可以进行操作的比较器。

public static <T extends an Object & make it Comparable<? super T>> T max(Collection of data <? extends T> coll)  
  or;
public static <T> T max(Collection of the data <? extends T> coll, Comparator<? super T> comparator)
登录后复制

Below approaches are useful for finding out the largest value in an array list −

  • 方法一 - 迭代方法

  • Approach 2 − Int method by Java 8 stream

  • Approach 3 − max() method

  • Approach 4 − Using ForEach Loop

  • Approach 5 − Using Library Function

By Using the Iteration Method

在这种方法中,时间复杂度是基于给定数据集的大小为0。而且不需要额外的辅助空间。

  • Recursive way to get max value.

  • Basic Condition of the method : if , (m==1) value return arr[0]

  • Else, get return the value of: maximum (arr[n-1], getmax(arr[], n-1))

Example

的中文翻译为:

示例

import java.util.*;  
public class CollectionsofmaxfileARRDD {  
   public static void main (String[] args) {  
      List<Integer> list = Arrays.asList(2010, 1010, 1001, 1400, 2501);  
      Integer max = Collections.max(list, Collections.reverseOrder());  
      System.out.println("Output from the particular string: "+max);  
   }  
}    
登录后复制

输出

Output from the particular string: 1001
登录后复制

通过在Java 8 Stream中使用Int方法

In this method the time complexity is totally 0 and the auxiliary space has no extra space needed because it is constant.

Example

的中文翻译为:

示例

import java.util.Arrays;
public class arbrdd {
   public static void main (String[] args){
      int arr[] = {07, 16, 10, 2001, 1997};
      int max = Arrays.stream(arr).max().getAsInt();
      System.out.println("Largest array is found from the array list" +max);
   }
}     
登录后复制

输出

Largest array is found from the array list2001
登录后复制

By Using the max() Method

通过使用max()方法,我们将使用以下过程构建Java代码 -

  • 声明带有最大值的变量

  • Initialize with the first element of an array

  • 运行循环

  • array[a]>maximum, set max = array[a]

  • 打印输出

Example

的中文翻译为:

示例

import java.util.*;
public class arbrdd{
   public static void main(String[] args){
      int arr[] = {10, 07, 16, 2001,1997};
      List<Integer> list = new ArrayList<>();
      for(int a=0;a<arr.length;a++){
         list.add(arr[a]);
      }
      System.out.println("Largest array present in the particular array list is " +Collections.max(list));
   }
}    
登录后复制

输出

Largest array present in the particular array list is 2001
登录后复制

通过使用ForEach循环

通过使用ForEach循环,我们将使用以下过程构建Java代码-

  • Call recursive say get max

  • 操作的基本条件:if,(a==1) 返回数组[0]

  • 否则,返回max(array[a-1], getmax(array, a-1))

Example

的中文翻译为:

示例

import java.util.Arrays;
import java.util.List;
public class maxarrayval {
   public static void main(String[] args){
      List<Integer> arrayList
      = Arrays.asList(10, 07, 16, 2001, 1997, 10052022);
      int maxValue0710 = Integer.MIN_VALUE;
      for (Integer integer : arrayList) {
         if (integer > maxValue0710)
         maxValue0710 = integer;
      }
      System.out.println("The maximum value present in the array is " + maxValue0710);
   }
}   
登录后复制

输出

The maximum value present in the array is 10052022
登录后复制

By Using Library Function

By using the library functions, here we will build a Java code by using the below process −

  • Maximum(arr,0,end)

  • 从该数组列表中读取倒数第二个元素

  • Find the larger element between 2nd last and last one from array data

  • Max value recursive iteration

  • 结束

Example

的中文翻译为:

示例

import java .io.*;
import java.util.*;
public class ARBRDD{
   static int largest(int []arr,int n){
      Arrays.sort(arr);
      return arr[n - 1];
   }
   static public void main (String[] args){
      int []arr = {07, 10, 2001,1997, 10052022};
      int n = arr.length;
      System.out.println(largest(arr, n));
   }
}   
登录后复制

输出

10052022
登录后复制

结论

In this article; today we learnt how to get the Largest Element in return from an array List using Java.

通过可能的条件和使用此处提到的逻辑编写的程序,我们如何使用数组循环,并根据所有可能的条件和此处编写的一些代码的处理过程来满足每个理论。

以上是Java程序返回列表中的最大元素的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!