目錄
To show you some instances
Instance-3
實例-1
Algorithm-2
演算法-2
Syntax
多種方法
方法一:使用for迴圈
Example
Output
Approach-2: By Using Arrays.sort
首頁 Java java教程 在Java中尋找兩個陣列元素的最大和

在Java中尋找兩個陣列元素的最大和

Sep 11, 2023 pm 02:13 PM
java 陣列 尋找 最大

在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]

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。

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

Java 中的平方根 Java 中的平方根 Aug 30, 2024 pm 04:26 PM

Java 中的平方根

Java 中的完美數 Java 中的完美數 Aug 30, 2024 pm 04:28 PM

Java 中的完美數

Java 中的隨機數產生器 Java 中的隨機數產生器 Aug 30, 2024 pm 04:27 PM

Java 中的隨機數產生器

Java中的Weka Java中的Weka Aug 30, 2024 pm 04:28 PM

Java中的Weka

Java 中的阿姆斯壯數 Java 中的阿姆斯壯數 Aug 30, 2024 pm 04:26 PM

Java 中的阿姆斯壯數

Java 中的史密斯數 Java 中的史密斯數 Aug 30, 2024 pm 04:28 PM

Java 中的史密斯數

Java Spring 面試題 Java Spring 面試題 Aug 30, 2024 pm 04:29 PM

Java Spring 面試題

突破或從Java 8流返回? 突破或從Java 8流返回? Feb 07, 2025 pm 12:09 PM

突破或從Java 8流返回?

See all articles