0

0

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

PHPz

PHPz

发布时间:2023-08-19 17:17:07

|

1365人浏览过

|

来源于tutorialspoint

转载

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).

    立即学习Java免费学习笔记(深入)”;

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 max(Collection of data  coll)  
  or;
public static  T max(Collection of the data  coll, Comparator 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

    Napkin AI
    Napkin AI

    Napkin AI 可以将您的文本转换为图表、流程图、信息图、思维导图视觉效果,以便快速有效地分享您的想法。

    下载
  • 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 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 list = new ArrayList<>();
      for(int a=0;a

输出

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 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
java

Java是一个通用术语,用于表示Java软件及其组件,包括“Java运行时环境 (JRE)”、“Java虚拟机 (JVM)”以及“插件”。php中文网还为大家带了Java相关下载资源、相关课程以及相关文章等内容,供大家免费下载使用。

832

2023.06.15

java正则表达式语法
java正则表达式语法

java正则表达式语法是一种模式匹配工具,它非常有用,可以在处理文本和字符串时快速地查找、替换、验证和提取特定的模式和数据。本专题提供java正则表达式语法的相关文章、下载和专题,供大家免费下载体验。

738

2023.07.05

java自学难吗
java自学难吗

Java自学并不难。Java语言相对于其他一些编程语言而言,有着较为简洁和易读的语法,本专题为大家提供java自学难吗相关的文章,大家可以免费体验。

734

2023.07.31

java配置jdk环境变量
java配置jdk环境变量

Java是一种广泛使用的高级编程语言,用于开发各种类型的应用程序。为了能够在计算机上正确运行和编译Java代码,需要正确配置Java Development Kit(JDK)环境变量。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

397

2023.08.01

java保留两位小数
java保留两位小数

Java是一种广泛应用于编程领域的高级编程语言。在Java中,保留两位小数是指在进行数值计算或输出时,限制小数部分只有两位有效数字,并将多余的位数进行四舍五入或截取。php中文网给大家带来了相关的教程以及文章,欢迎大家前来阅读学习。

398

2023.08.02

java基本数据类型
java基本数据类型

java基本数据类型有:1、byte;2、short;3、int;4、long;5、float;6、double;7、char;8、boolean。本专题为大家提供java基本数据类型的相关的文章、下载、课程内容,供大家免费下载体验。

446

2023.08.02

java有什么用
java有什么用

java可以开发应用程序、移动应用、Web应用、企业级应用、嵌入式系统等方面。本专题为大家提供java有什么用的相关的文章、下载、课程内容,供大家免费下载体验。

430

2023.08.02

java在线网站
java在线网站

Java在线网站是指提供Java编程学习、实践和交流平台的网络服务。近年来,随着Java语言在软件开发领域的广泛应用,越来越多的人对Java编程感兴趣,并希望能够通过在线网站来学习和提高自己的Java编程技能。php中文网给大家带来了相关的视频、教程以及文章,欢迎大家前来学习阅读和下载。

16925

2023.08.03

Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
最新Python教程 从入门到精通
最新Python教程 从入门到精通

共4课时 | 0.7万人学习

Node.js 教程
Node.js 教程

共57课时 | 8.6万人学习

CSS3 教程
CSS3 教程

共18课时 | 4.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号