# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Sum of Two Arrays Elements in Java | Array Programs in Java – 7 | In the previous Java program, we find the average of an array. Now in this post, we will develop a program to find the sum of two arrays elements in Java.
Program Description:- Write a Java program to find the sum of two arrays elements.
To calculate the sum of two arrays element by element in Java both arrays must be of equal type and equal size. If they have different types or different sizes then we will get IllegalArgumentException. To solve this problem we have to create a third array of the same size and then store the sum of corresponding elements of the given arrays.
Note that we can’t add two arrays that are of different types or incompatible types. Both arrays should be similar types or compatible with each other.
Example:-
array1[]
= {10, 20, 30, 40, 50};
array2[]
= {9, 18, 27, 36, 45};
The resultant array will be,
array3[]
= {19, 38, 57, 76, 95};
And it was calculated as,
array3[]
= {10+9, 20+18, 30+27, 40+36, 50+45};
Program to Find Sum of Two Arrays in Java
import java.util.Scanner;
import java.util.Arrays;
public class TwoArraySum {
public static void main(String[] args) {
// create Scanner class object
Scanner scan = new Scanner(System.in);
// take number of elements in both array
System.out.print("Enter number of elements in first array: ");
int array1size = scan.nextInt();
System.out.print("Enter number of elements in second array: ");
int array2size = scan.nextInt();
// both array must have same number of elements
if(array1size != array2size) {
System.out.println("Both array must have "+
"same number of elements");
return;
}
// declare three array with given size
int array1[] = new int[array1size];
int array2[] = new int[array1size];
int array3[] = new int[array1size];
// take input for array1 elements
System.out.println("Enter first array elements: ");
for (int i=0; i<array1.length; i++) {
array1[i] = scan.nextInt();
}
// take input for array2 elements
System.out.println("Enter second array elements: ");
for (int i=0; i<array2.length; i++) {
array2[i] = scan.nextInt();
}
// loop to iterate through the array
for (int i=0; i<array3.length; i++) {
// add array elements
array3[i] = array1[i] + array2[i];
}
// display the third array
System.out.println("Resultant Array: "
+ Arrays.toString(array3));
}
}
Output for the different test cases:-
Enter number of elements in first array: 5
Enter number of elements in second array: 5
Enter first array elements:
10 20 30 40 50
Enter second array elements:
9 18 27 36 45
Resultant Array: [19, 38, 57, 76, 95]
Enter number of elements in first array: 7
Enter number of elements in second array: 3
Both array must have same number of elements
In the above program, first, we created a Scanner class object to take input from the end-user. Then we asked the user to input the number of elements in both arrays. See more:- How to take input for an array from the end-user.
If the number of elements in both arrays is different then we can never find the sum of two arrays element by element. In that case, display the message, stop the execution of the main method, and send control back to the caller method.
Otherwise, take both arrays as input values from the end-user and store them in an appropriate array. Now, iterate through these arrays, calculate the sum of arrays element by element, and store them in the third array.
Finally, display the resultant array. To display them you can use normally for loop or for-each loop, iterate through them, and display the elements of the third. But we used Arrays.toString(array3) to do the same task. In the Arrays class, the toString() method is overridden to display the content of the array rather than its class name and hashcode value.
The sum of Two Arrays Using Stream
import java.util.Arrays;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
int[] array1 = { 10, 20, 30, 40, 50 };
int[] array2 = { 9, 18, 27, 36, 45 };
int[] sumArray = new int[array1.length];
IntStream.range(0, sumArray.length)
.forEach(i -> sumArray[i] = array1[i] + array2[i]);
System.out.println(Arrays.toString(sumArray));
}
}
The same can be done as follows:-
import java.util.Arrays;
import java.util.stream.IntStream;
public class Test {
public static void main(String[] args) {
int[] array1 = { 10, 20, 30, 40, 50 };
int[] array2 = { 9, 18, 27, 36, 45 };
int[] sumArray = IntStream.range(0, array1.length)
.map(i -> array1[i] + array2[i])
.toArray();
System.out.println(Arrays.toString(sumArray));
}
}
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!