首页 > Java > java教程 > 正文

在Java中查找两个数组元素的最大和

PHPz
发布: 2023-09-11 14:13:02
转载
1487人浏览过

在java中查找两个数组元素的最大和

Two elements giving the maximum sum in an array means, we have to find two largest array elements which will eventually give the maximum sum possible.

In this article we will see how we can find the maximum sum of two elements in Java.

To show you some instances

Instance-1

的中文翻译为:

实例-1

Suppose we have the below array

[10, 2, 3, -5, 99, 12, 0, -1]

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

In this array the largest element is 99 and second largest is 12.

Max sum = 99 + 12

因此,这个数组中两个元素的最大和为111。

Instance-2

Suppose we have the below array

[556, 10, 259, 874, 123, 453, -96, -54, -2369]

在这个数组中,最大的元素是874,第二大的元素是556。

Max sum = 874+556

因此,该数组中两个元素的最大和为1430。

Instance-3

Suppose we have the below array

[55, 10, 29, 74, 12, 45, 6, 5, 269]

在这个数组中,最大的元素是269,第二大的元素是74。

Max sum= 269+74

因此,该数组中两个元素的最大和为343。

落笔AI
落笔AI

AI写作,AI写网文、AI写长篇小说、短篇小说

落笔AI 41
查看详情 落笔AI

Algorithm

算法-1

步骤 1 − 使用 for 循环在数组中找到最大和第二大的元素。

步骤 2 - 找到它们的和。

Step 3 − Print the sum.

Algorithm-2

的翻译为:

算法-2

Step 1 − Sort the array elements.

Step 2 −Take the last and second last element of the array.

Step 3 − Find their sum.

Step 4 − Print the sum.

Syntax

To sort the array we need to use the sort( ) method of the Arrays class of java.util package.

Following is the syntax to sort any array in ascending order using the method

<span class="typ">Arrays</span><span class="pun">.</span><span class="pln">sort</span><span class="pun">(</span><span class="pln">array_name</span><span class="pun">);</span>
登录后复制

Where, ‘array_name’ refers to the array that you want to sort.

多种方法

We have provided the solution in different approaches.

  • 使用for循环找到最大的和

  • Find The Largest Sum Using Arrays.sort

让我们逐一查看程序及其输出。

方法一:使用for循环

In this approach, we use a for loop to iterate through the array elements to find out the largest and second largest element. These two elements will give the maximum sum.

Example

public class Main {
   public static void main(String[] args) {
      // The array elements
      int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 };

      // Storing the first element in both variables
      int first = arr[0], second = arr[0];

      // For loop to iterate the elements from 1 to n
      // to find the first largest element
      for (int i = 0; i < arr.length; i++) {

         // If array element is larger than current largest element, then swap
         if (arr[i] > first)
         first = arr[i];
      }

      // For loop to iterate the elements from 1 to n
      // to find the second largest element
      for (int i = 0; i < arr.length; i++) {

         // If array element is larger than current largest element and not equals to
         // largest element, then swap
         if (arr[i] > second && arr[i] != first)
            second = arr[i];
      }

      // Print the sum
      System.out.println("Largest sum = " + (first + second));
      System.out.println("The elements are " + first + " and " + second);
   }
}
登录后复制

Output

Largest sum = 111
The elements are 99 and 12
登录后复制

Approach-2: By Using Arrays.sort

在这种方法中,我们使用Arrays.sort()方法对数组进行排序。然后我们取最后一个和倒数第二个索引上的元素。由于数组已经排序过了,这两个元素将给出最大的和。

Example

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
      // The array elements
      int arr[] = { 10, 2, 3, -5, 99, 12, 0, -1 };

      // Sort the array using the sort method from array class
      Arrays.sort(arr);

      // Storing the last element as largest and second last element as second largest
      int first = arr[arr.length - 1], second = arr[arr.length - 2];

      // Print the maximum sum
      System.out.println("Maximum sum = " + (first + second));
      System.out.println("The elements are " + first + " and " + second);
   }
}
登录后复制

Output

Maximum sum = 104
The elements are 99 and 12
登录后复制

在本文中,我们探讨了在Java中找到数组中具有最大和的两个元素的不同方法。

以上就是在Java中查找两个数组元素的最大和的详细内容,更多请关注php中文网其它相关文章!

java速学教程(入门到精通)
java速学教程(入门到精通)

java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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