Calculating the Sum of Two Arrays Element by Element in Java (#14579)
* Create SumArraysUsingForLoop.java * Update SumArraysUsingForLoop.java * add sum arrays using for each loop and streams * Create SumArraysUsingForLoopTest.java * Add files via upload * Update SumArraysUsingStreams.java * Update and rename SumArraysUsingForEachTest.java to SumArraysUsingForEachUnitTest.java * Rename SumArraysUsingForLoopTest.java to SumArraysUsingForLoopUnitTest.java * Update and rename SumArraysUsingStreamsTest.java to SumArraysUsingStreamsUnitTest.java * Update SumArraysUsingForEachUnitTest.java * Update SumArraysUsingForLoopUnitTest.java * Update SumArraysUsingForEachUnitTest.java * Update SumArraysUsingStreamsUnitTest.java
This commit is contained in:
parent
b25e324790
commit
8502670140
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.arraysums;
|
||||
|
||||
public class SumArraysUsingForEach {
|
||||
|
||||
public static int[] sumOfTwoArrays(int[] arr1, int[] arr2) {
|
||||
int[] arr3 = new int[arr1.length];
|
||||
int counter = 0;
|
||||
for (int num1 : arr1) {
|
||||
arr3[counter] = num1 + arr2[counter];
|
||||
counter++;
|
||||
}
|
||||
return arr3;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
package com.baeldung.arraysums;
|
||||
|
||||
public class SumArraysUsingForLoop {
|
||||
|
||||
public static int[] sumOfTwoArrays(int[] arr1, int[] arr2) {
|
||||
int[] arr3 = new int[arr1.length];
|
||||
for (int i = 0; i < arr1.length; i++) {
|
||||
arr3[i] = arr1[i] + arr2[i];
|
||||
}
|
||||
return arr3;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package com.baeldung.arraysums;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class SumArraysUsingStreams {
|
||||
|
||||
public static int[] sumOfTwoArrays(int[] arr1, int[] arr2) {
|
||||
IntStream range = IntStream.range(0, Math.min(arr1.length, arr2.length));
|
||||
IntStream stream3 = range.map(i -> arr1[i] + arr2[i]);
|
||||
int[] arr3 = stream3.toArray();
|
||||
return arr3;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.arraysums;
|
||||
|
||||
import com.baeldung.arraysums.SumArraysUsingForEach;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SumArraysUsingForEachUnitTest {
|
||||
|
||||
@Test
|
||||
public void sumOfTwoArraysUsingForEach_GivenTwoEqualSizedIntArrays_ReturnsCorrectSumArray() {
|
||||
int[] arr1 = { 4, 5, 1, 6, 4, 15 };
|
||||
int[] arr2 = { 3, 5, 6, 1, 9, 6 };
|
||||
int[] expected = { 7, 10, 7, 7, 13, 21 };
|
||||
assertArrayEquals(expected, SumArraysUsingForEach.sumOfTwoArrays(arr1, arr2));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.arraysums;
|
||||
|
||||
import com.baeldung.arraysums.SumArraysUsingForLoop;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SumArraysUsingForLoopUnitTest {
|
||||
|
||||
@Test
|
||||
public void sumOfTwoArrays_GivenTwoEqualSizedIntArrays_ReturnsCorrectSumArray() {
|
||||
int[] arr1 = {4, 5, 1, 6, 4, 15};
|
||||
int[] arr2 = {3, 5, 6, 1, 9, 6};
|
||||
int[] expected = {7, 10, 7, 7, 13, 21};
|
||||
assertArrayEquals(expected, SumArraysUsingForLoop.sumOfTwoArrays(arr1, arr2));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.arraysums;
|
||||
|
||||
import com.baeldung.arraysums.SumArraysUsingStreams;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class SumArraysUsingStreamsUnitTest {
|
||||
|
||||
@Test
|
||||
public void sumOfTwoArraysUsingStreams_GivenTwoEqualSizedIntArrays_ReturnsCorrectSumArray() {
|
||||
int[] arr1 = {4, 5, 1, 6, 4, 15};
|
||||
int[] arr2 = {3, 5, 6, 1, 9, 6};
|
||||
int[] expected = {7, 10, 7, 7, 13, 21};
|
||||
assertArrayEquals(expected, SumArraysUsingStreams.sumOfTwoArrays(arr1, arr2));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue