How to add two arrays in java: In the previous article, we have seen Java Program to Concatenate Two Arrays
In this article we will see how to find sum of two arrays.
Java Program to Find Sum of Two Arrays Elements
Add two arrays java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.
In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.
Declaration of an array:
dataType[] arrayName; (or) //Declaring an array dataType []arrayName; (or) dataType arr[];
Instantiation of an Array:
arrayName = new datatype[size]; //Allocating memory to array
Combining both Statements in One:
dataType[] arrayName = new dataType[size] //Declaring and Instantiating array
Initialization of an Array:
arrayName[index-0]= arrayElement1 //Initializing the array ... arrayName[index-s]= arrayElementS
Combining all Statements in One:
dataType arrayName[ ]={e1,e2,e3}; //declaration, instantiation and initialization
Let’s see different ways to add elements of two arrays accordingly.
Method-1: Java Program to Find Sum of Two Arrays Elements By Static Initialization of Array Elements
Approach:
- Take two arrays of similar size with elements.
- Display both arrays.
- Create another empty array of the same size.
- Use a for loop to add the elements and store it in the empty array.
- Display the array.
Program:
import java.util.*; public class Main { public static void main(String args[]) { //Original array int arr1[] = {12, 22, 34, 22, 54}; int arr2[] = {29, 54, 98, 87, 2}; // Printing both arras printArray(arr1); printArray(arr2); int resultantArr[] = new int[arr1.length]; // Using for loop to add the elements from arr1 and arr2 for(int i=0;i<arr1.length;i++) resultantArr[i] = arr1[i] + arr2[i]; // Printing the resultant array printArray(resultantArr); } // Function to print the array static void printArray(int arr[]) { System.out.println("The array elements are"); for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } }
Output: The array elements are 12 22 34 22 54 The array elements are 29 54 98 87 2 The array elements are 41 76 132 109 56
Method-2: Java Program to Find Sum of Two Arrays Elements By Dynamic Initialization of Array Elements
Approach:
- Create scanner class object.
- Ask user for length of the array.
- Declare two arrays with same size.
- Ask the user for input array elements for both the arrays.
- Display both arrays.
- Create another empty array of the same size.
- Use a for loop to add the elements and store it in the empty array.
- Display the array.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter the number of elements in the array: "); int num = sc.nextInt(); //Declaring both the arrays with size 'num' int arr1[] = new int[num]; int arr2[] = new int[num]; //Taking input of array elements for arr1 System.out.print("Enter the elements for arr1: "); for (int i = 0; i < num; i++) { arr1[i] = sc.nextInt(); } //Taking input of array elements for arr1 System.out.print("Enter the elements for arr2: "); for (int i = 0; i < num; i++) { arr2[i] = sc.nextInt(); } // Printing both arras printArray(arr1); printArray(arr2); int resultantArr[] = new int[arr1.length]; // Using for loop to add the elements from arr1 and arr2 for(int i=0;i<arr1.length;i++) resultantArr[i] = arr1[i] + arr2[i]; // Printing the resultant array printArray(resultantArr); } // Function to print the array static void printArray(int arr[]) { System.out.println("After adding two arrays the array elements are"); for(int i = 0; i < arr.length ; i++) System.out.print(arr[i]+" "); System.out.println(); } }
Output: Enter the number of elements in the array: 6 Enter the elements for arr1: 11 56 23 89 65 81 Enter the elements for arr2: 44 66 95 25 67 19 After adding two arrays the array elements are 11 56 23 89 65 81 After adding two arrays the array elements are 44 66 95 25 67 19 After adding two arrays the array elements are 55 122 118 114 132 100
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: