본문
[Java] double array to int array
프로그래밍/Java 2021. 3. 19. 09:15
double array에서 int array로 변경하고 싶을때
you need to create a new array of int(s) and assign the result of your Math.ceil calls (with an appropriate cast). Like,
public static int[] roundUp(double[] array2) {
int[] arr = new int[array2.length];
for (int i = 0; i < array2.length; i++) {
arr[i] = (int) Math.ceil(array2[i]);
}
return arr;
}
If you're using Java 8+, you could also use a DoubleStream and map each element to an int before converting to an array in one line. Like,
public static int[] roundUp2(double[] array2) {
return DoubleStream.of(array2).mapToInt(d -> (int) Math.ceil(d)).toArray();
}
https://stackoverflow.com/users/2970947/elliott-frisch
댓글