
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of Two Array Elements Index Wise in Java
In Java, Array is an object. It is a non-primitive data type which stores values of similar data type.
As per the problem statement we have to find the sum of two different arrays with respect to index and store it into a third array. Suppose a1[] is first array, a2[] is second array and a3[] is third array, then sum of a1[] and a2[] should be stored in a3[] i.e.
a1[0] + a2[0] = a3[0] a1[1] + a2[1] = a3[1] a1[2] + a2[2] = a3[2] and so on.
Let's start!
Note ? Two array lengths must be the same and array elements should be numeric.
In this article, you will see how to find the sum of two array elements with respect to its index positions and store them in another array by using Java programming language.
Let's start.
To Show You Some Instances
Instance-1
Suppose a1[] array is {5, 6, 3, 2, 4, 11} and a2[] array is {3, 9, 5, 21, 19, 2}
After adding the two arrays, the result will be ?
Resultant array is: [8, 15, 8, 23, 23, 13]
Instance-2
Suppose a1[] array is {9, 6, 1, 2, 41, 21} and a2[] array is {3, 9, 8, 31, 9, 42}
After adding the two arrays, the result will be ?
Resultant array is: [12, 15, 9, 33, 50, 63]
Instance-3
Suppose a1[] array is {51, 16, 33, 2, 14, 21} and a2[] array is {3, 9, 8, 31, 9, 42}
After adding the two arrays, the result will be ?
Resultant array is: [84, 25, 89, 23, 53, 42]
Algorithm
Step 1 ? Declare and initialize an integer array.
Step 2 ? Check if the length of both arrays is equal or not.
Step 3 ? If the length of both arrays is equal then add them using "a1[] + a2[] = a3[]".
Step 4 ? Print the resultant array
Step 5 ? Otherwise print "Length of both arrays should be same".
Syntax
To get the length of an array (number of elements in that array), there is an inbuilt property of array i.e length.
Below refers to the syntax of it ?
array.length
where, ?array' refers to the array reference.
Multiple Approaches
We have provided the solution in different approaches.
By Using Static Initialization of Array.
By Using User Defined Method.
Let's see the program along with its output one by one.
Approach-1: By Using Static Initialization of Array
Example
In this approach, array elements will be initialized in the program. Then as per the algorithm find the sum of two array elements with respect to the index and store it in another array.
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"); } } }
Output
Resultant array is: [84, 25, 89, 23, 53, 42]
Approach-2: By Using User Defined Method
Example
In this approach, array elements will be initialized in the program. Then call a user-defined method by passing the array as a parameter and inside the method as per the algorithm find the sum of two array elements with respect to the index and store it in another array.
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"); } } }
Output
Resultant array is: [12, 15, 9, 33, 50, 63]
In this article, we explored how to find the sum of two array elements with respect to its index and store the values in another array by using Java programming language.