
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
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.
- Related Articles
- Find Two Array Elements Having Maximum Sum in Java?
- Minimum Index Sum for Common Elements of Two Lists in C++
- Java program to find the sum of elements of an array
- Find Two Array Elements Having Maximum Product in Java?
- Find if sum of odd or even array elements are smaller in Java
- Digits of element wise sum of two arrays into a new array in C++ Program
- Find the Equilibrium Index of an array in Java?
- How to sum elements at the same index in array of arrays into a single array? JavaScript
- How to Find Sum of Matrix Elements in Java?
- Reverse index value sum of array in JavaScript
- Compute the bit-wise NOT of a Two-Dimensional array element-wise in Numpy
- Alternating sum of elements of a two-dimensional array using JavaScript
- Minimum Index Sum of Two Lists in C++
- How to Alter Two Array Elements in Java
- How to find the sum of all array elements in R?
