Skip to content

Commit 13942c0

Browse files
authored
Update Java8MaxMinUnitTest.java (#18900)
1 parent 09ce903 commit 13942c0

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

core-java-modules/core-java-collections-list/src/test/java/com/baeldung/java8/Java8MaxMinUnitTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,23 @@ public void givenIntegerList_whenGetMaxAbsolute_thenReturnMaxAbsolute() {
106106
assertEquals(-10, absMax);
107107
}
108108

109+
private static int findMaxRecursive(int[] array, int n) {
110+
if (n == 1) {
111+
return array[0];
112+
}
113+
return Math.max(array[n - 1], findMaxRecursive(array, n - 1));
114+
}
115+
116+
@Test
117+
public void givenIntegerArray_whenFindingMaxUsingRecursion_thenMaxCanBeFoundUsingRecursion() {
118+
// given
119+
int[] integers = new int[]{20, 98, 12, 7, 35};
120+
int expectedMax = 98;
121+
122+
// when
123+
int max = findMaxRecursive(integers, integers.length);
124+
125+
// then
126+
assertEquals(expectedMax, max);
127+
}
109128
}

0 commit comments

Comments
 (0)