Skip to content

Commit acb72d8

Browse files
committed
KNearestNumbers: use int[] for param and return type for consistency
1 parent 24719fb commit acb72d8

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

Diff for: src/main/java/by/andd3dfx/numeric/KNearestNumbers.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package by.andd3dfx.numeric;
22

33
import java.util.ArrayList;
4-
import java.util.List;
54

65
/**
76
* Find K nearest numbers for item placed on i-th position in array of numbers.
@@ -24,7 +23,7 @@
2423
*/
2524
public class KNearestNumbers {
2625

27-
public static List<Integer> find(int[] nums, int i, int k) {
26+
public static int[] find(int[] nums, int i, int k) {
2827
if (i < 0 || i >= nums.length) {
2928
throw new IllegalArgumentException("Index `i` is out of array's range!");
3029
}
@@ -33,7 +32,7 @@ public static List<Integer> find(int[] nums, int i, int k) {
3332
}
3433

3534
if (k == 0) {
36-
return List.of();
35+
return new int[]{};
3736
}
3837
var result = new ArrayList<Integer>();
3938
result.add(nums[i]);
@@ -52,7 +51,9 @@ public static List<Integer> find(int[] nums, int i, int k) {
5251
right++;
5352
}
5453
}
55-
return result;
54+
return result.stream()
55+
.mapToInt(value -> value)
56+
.toArray();
5657
}
5758

5859
private static int determineLeftDistance(int[] nums, int left, int curr) {

Diff for: src/test/java/by/andd3dfx/numeric/KNearestNumbersTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package by.andd3dfx.numeric;
22

3-
import org.apache.commons.collections4.CollectionUtils;
43
import org.junit.Test;
54

6-
import java.util.List;
5+
import java.util.Arrays;
76

87
import static by.andd3dfx.numeric.KNearestNumbers.find;
98
import static org.assertj.core.api.Assertions.assertThat;
@@ -21,8 +20,8 @@ public void testFind() {
2120

2221
var result = find(new int[]{1, 2, 3, 4, 5, 6}, 3, 2);
2322
assertTrue(
24-
CollectionUtils.isEqualCollection(result, List.of(4, 3)) ||
25-
CollectionUtils.isEqualCollection(result, List.of(4, 5))
23+
Arrays.equals(result, new int[]{4, 3}) ||
24+
Arrays.equals(result, new int[]{4, 5})
2625
);
2726

2827
assertThat(find(new int[]{1, 2, 3, 4, 5, 6}, 0, 3))

0 commit comments

Comments
 (0)