Java program to find the largest element in array

Given an array of numbers, write a java program to find the largest element in the given array.

Example:

int[] a = {1, 5, 3, 9, 2, 8, 2}
Largest Element: 9

Java 代码

public class LargestElementInArray {
public static void findLargestElement(int[] a) {
if (a == null || a.length == 0) {
return;
}
int largest_element = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > largest_element) {
largest_element = a[i];
}
}
System.out.println("Largest element in array: " + largest_element);
} public static void main(String[] args) {
int[] a = {1, 5, 3, 9, 2, 8, 2};
findLargestElement(a);
}
}

Java program to find the largest element in array的更多相关文章

  1. Java for LeetCode 215 Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  2. LeetCode Kth Largest Element in an Array

    原题链接在这里:https://leetcode.com/problems/kth-largest-element-in-an-array/ 题目: Find the kth largest elem ...

  3. Kth Largest Element in an Array 解答

    Question Find the kth largest element in an unsorted array. Note that it is the kth largest element ...

  4. Lintcode: Kth Largest Element 解题报告

    Kth Largest Element Find K-th largest element in an array. Note You can swap elements in the array E ...

  5. LeetCode赛题515----Find Largest Element in Each Row

    问题描述 You need to find the largest element in each row of a Binary Tree. Example: Input: 1 / \ 2 3 / ...

  6. 【leetcode】215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  7. Kth Largest Element in a Stream

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  8. 堆排序 && Kth Largest Element in an Array

    堆排序 堆节点的访问 通常堆是通过一维数组来实现的.在数组起始位置为0的情形中: 父节点i的左子节点在位置(2*i+1); 父节点i的右子节点在位置(2*i+2); 子节点i的父节点在位置floor( ...

  9. [LeetCode] Kth Largest Element in an Array 数组中第k大的数字

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

随机推荐

  1. 数据结构实验之排序五:归并求逆序数(SDUT 3402)

    归并排序详解(戳我). 以下是搬了别人的. #include<stdio.h> #include<stdlib.h> long long sum = 0; int a[1000 ...

  2. PHP连接MySQL创建表

    源代码: <?php header("Content-Type:text/html;charset=utf8");//声明编码格式 $conn=mysqli_connect( ...

  3. Java 基础:单例模式 Singleton Pattern

    1.简介 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...

  4. 小程序中怎么引入wepy.js第三方toast组件

    1.先安装组件 npm install wepy-com-toast --save 2.引入wepy和toast import wepy from 'wepy' import Toast from ' ...

  5. IPV4 VS IPV6 谈谈省级ipv6的必要性

    11月26日,中办.国办印发了<推进互联网协议第六版(IPv6)规模部署行动计划>,提出国内要在 5~10 年的时间形成下一代互联网自主技术体系和产业生态,建成全球最大规模的 IPv6 商 ...

  6. Spring boot Aop 示例

    需要的依赖 <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -- ...

  7. Apollo源码打包及部署

    1. 通过源码打包 到携程Apollo地址 https://github.com/ctripcorp/apollo 下载Apollo源码,可在源码中进行自定义配置日志路径及端口等,之后打包. 打包完成 ...

  8. Java基础 import 要在所有的class前面

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  9. django实战总结2

    https://www.jianshu.com/p/9b3bfe934511 https://www.cnblogs.com/1Q84mi/p/xadmin002.html https://blog. ...

  10. python中修改列表元素的方法

    一.在for循环中直接更改列表中元素的值不会起作用: 如: l = list(range(10)[::2]) print (l) for n in l: n = 0 print (l) 运行结果: [ ...