在Java中查詢兩個陣列元素的索引級和


在Java中,陣列是一個物件。它是一種非原始資料型別,用於儲存相同資料型別的值。

根據問題陳述,我們必須找到兩個不同陣列的索引級和,並將其儲存到第三個陣列中。假設 a1[] 是第一個陣列,a2[] 是第二個陣列,a3[] 是第三個陣列,則 a1[] 和 a2[] 的和應儲存在 a3[] 中,即

a1[0] + a2[0] = a3[0]
a1[1] + a2[1] = a3[1]
a1[2] + a2[2] = a3[2] and so on.

讓我們開始吧!

注意 - 兩個陣列的長度必須相同,並且陣列元素必須是數字。

在本文中,您將看到如何使用Java程式語言查詢兩個陣列元素相對於其索引位置的和,並將它們儲存到另一個數組中。

讓我們開始。

向您展示一些例項

例項-1

Suppose a1[] array is {5, 6, 3, 2, 4, 11} and a2[] array is {3, 9, 5, 21, 19, 2}

新增兩個陣列後,結果將為 -

Resultant array is: [8, 15, 8, 23, 23, 13] 

例項-2

Suppose a1[] array is {9, 6, 1, 2, 41, 21} and a2[] array is {3, 9, 8, 31, 9, 42}

新增兩個陣列後,結果將為 -

Resultant array is: [12, 15, 9, 33, 50, 63]

例項-3

Suppose a1[] array is {51, 16, 33, 2, 14, 21} and a2[] array is {3, 9, 8, 31, 9, 42}

新增兩個陣列後,結果將為 -

Resultant array is: [84, 25, 89, 23, 53, 42]

演算法

  • 步驟 1 - 宣告並初始化一個整數陣列。

  • 步驟 2 - 檢查兩個陣列的長度是否相等。

  • 步驟 3 - 如果兩個陣列的長度相等,則使用“a1[] + a2[] = a3[]”將它們相加。

  • 步驟 4 - 列印結果陣列

  • 步驟 5 - 否則列印“兩個陣列的長度應該相同”。

語法

要獲取陣列的長度(該陣列中的元素數量),陣列有一個內建屬性,即length。

下面指的是它的語法 -

array.length

其中,'array' 指的是陣列引用。

多種方法

我們提供了不同方法的解決方案。

  • 透過使用陣列的靜態初始化。

  • 透過使用使用者定義的方法。

讓我們逐一檢視程式及其輸出。

方法-1:透過使用陣列的靜態初始化

示例

在這種方法中,陣列元素將在程式中初始化。然後根據演算法找到兩個陣列元素相對於索引的和,並將其儲存到另一個數組中。

import java.util.*;
public class Main {

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements
      int[] a = {51, 16, 33, 2, 14, 21};
      int[] b = {33, 9, 56, 21, 39, 21};

      //get length of an array and store it in c array 
      int[] c = new int[a.length];

      //check if length of both array are equal
      if(a.length==b.length){
         //logic implementation
         for (int i = 0 ,j=0,k=0; i < a.length; i++,j++,k++){
            c[k] = a[i] + b[j];
         }

         //Print the result
         System.out.println("Resultant array is:");
         System.out.println(Arrays.toString(c));
      } else {
         System.out.println("Length of both array should be same");
      }
   }
}

輸出

Resultant array is:
[84, 25, 89, 23, 53, 42]

方法-2:透過使用使用者定義的方法

示例

在這種方法中,陣列元素將在程式中初始化。然後透過將陣列作為引數傳遞來呼叫使用者定義的方法,並在方法內部根據演算法找到兩個陣列元素相對於索引的和,並將其儲存到另一個數組中。

import java.util.*;
public class Main{

   //main method
   public static void main(String[] args){
   
      //Declare and initialize the array elements    
      int[] a = {9, 6, 1, 2, 41, 21};
      int[] b = {3, 9, 8, 31, 9, 42};
      
      //calling user defined method
      add(a, b);   
   }
   
   //user defined method
   public static void add(int []a, int []b){
   
      //get length of an array and store it in c array
      int[] c = new int[a.length];

      //check if length of both array are equal
      if(a.length==b.length){
      
         //logic implementation
         for (int i = 0 ,j=0,k=0; i < a.length; i++,j++,k++){
            c[k] = a[i] + b[j];
         }

         //Print the result
         System.out.println("Resultant array is:");
         System.out.println(Arrays.toString(c));
      } else {
         System.out.println("Length of both array should be same");
      }
   }
}

輸出

Resultant array is:
[12, 15, 9, 33, 50, 63]

在本文中,我們探討了如何使用Java程式語言查詢兩個陣列元素相對於其索引的和,並將值儲存到另一個數組中。

更新於:2023年1月5日

18K+ 瀏覽量

開啟你的職業生涯

透過完成課程獲得認證

開始
廣告

© . All rights reserved.