Type-1:
Here's a Java program to find the element-wise sum of two arrays of the same length:
public class ArrayElementWiseSum {
public static void main(String[] args) {
// Step 1: Define two arrays of the same length
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 5, 4, 3, 2, 1 };
// Step 2: Find the element-wise sum and store it in a new array
int[] sumArray = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
sumArray[i] = array1[i] + array2[i];
}
// Step 3: Print the element-wise sum array
System.out.println("Element-wise Sum Array: ");
for (int number : sumArray) {
System.out.print(number + " ");
}
}
}
Explanation of the program:
- We start by defining two integer arrays named array1 and array2. Both arrays have the same length, as mentioned in the problem statement.
- We then create a new integer array named sumArray to store the element-wise sum of array1 and array2.
- We use a traditional for loop to iterate through the arrays. At each index i, we add the corresponding elements from array1 and array2 and store the result in the sumArray.
- After the loop has finished, the sumArray will contain the element-wise sum of array1 and array2.
- Finally, we print the sumArray to display the element-wise sum of the two arrays.
When you run this program with the provided arrays, it will output:
Element-wise Sum Array:
6 6 6 6 6
This is because the element-wise sum of {1, 2, 3, 4, 5} and {5, 4, 3, 2, 1} is {6, 6, 6, 6, 6}. If you modify the arrays array1 and array2, the program will calculate and display the new element-wise sum accordingly.
Type-2:
Another way to find the element-wise sum of two arrays of the same length is by using Java Streams. Here's the Java program using this approach:
import java.util.Arrays;
public class ArrayElementWiseSum {
public static void main(String[] args) {
// Step 1: Define two arrays of the same length
int[] array1 = { 1, 2, 3, 4, 5 };
int[] array2 = { 5, 4, 3, 2, 1 };
// Step 2: Find the element-wise sum using Java Streams
int[] sumArray = Arrays.stream(array1)
.mapToObj((num, index) -> num + array2[index])
.mapToInt(Integer::intValue)
.toArray();
// Step 3: Print the element-wise sum array
System.out.println("Element-wise Sum Array: ");
for (int number : sumArray) {
System.out.print(number + " ");
}
}
}
Explanation of the alternative method:
- We start by defining two integer arrays named array1 and array2, just like in the previous example.
- We use Java Streams to find the element-wise sum of the two arrays. The Arrays.stream(array1) converts the array1 into a Stream of integers.
- We use the mapToObj() method to perform the element-wise sum. For each element num at a specific index, we add the corresponding element from array2 (accessed using array2[index]). This operation results in a Stream of Integer objects.
- Next, we use the mapToInt() method to convert the Stream of Integer objects back to a Stream of primitive integers.
- Finally, we use the toArray() method to convert the Stream of primitive integers into an array of integers, which gives us the element-wise sum in the sumArray.
- Finally, we print the sumArray to display the element-wise sum of the two arrays.
When you run this program with the provided arrays, it will output the same result as before:
Element-wise Sum Array:
6 6 6 6 6
This is because the element-wise sum of {1, 2, 3, 4, 5} and {5, 4, 3, 2, 1} is {6, 6, 6, 6, 6}. If you modify the arrays array1 and array2, the program will calculate and display the new element-wise sum accordingly. Using Java Streams provides a concise and expressive way to perform operations on arrays or collections.
0 Comments