How do I cast an array of doubles into an array of integers in Java?

Dan H. :

This is my homework question: "Create a method called roundAllUp(), which takes in an array of doubles and returns a new array of integers. This returned array contains all the numbers from the array of doubles, but rounded up."

Here is the method I have so far:

public static int[] roundUp(double[] array2){
    for(int i = 0; i < array2.length; i++){
        Math.ceil(array2[i]);
    }
}

Once the values are rounded up from the array of doubles, how can I return them in an array of integers?

Elliott Frisch :

You're close, 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();
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I cast a Java Object into a List of integers?

How do I convert an array of integers to binary?

How can I generate a list or array of sequential integers in Java?

How do I find out if a numpy array contains integers?

How can I generate a list or array of sequential integers in Java?

How do I create a primitive two-dimensional (2d) array of doubles in Clojure?

How to convert array of floats to array of doubles in Java?

I want to format an array of integers. How do I send the array so that each verb receives a different element of the array?

How do I validate an array of integers in Laravel

How do I convert an array of integers to an array of objects?

How do I initialize an integer array in LLVM using a list of integers?

How do I generate random doubles in an Array?

How do I pass an array of integers to a Postgres stored procedure with pqxx?

How do I efficiently map integers to URIs in a multidemnsional array?

How do I convert an array of floats into an array of unique integers?

How do I cast a multidimensional json array to a multidimensional dart list?

Cast an Arraylist of Doubles to ArrayList of Integers

How Do I Store Integers from File of Mixed Types into an Array? Java

How to create a list of doubles out of an array of doubles

How do I store, sort and print a 2D "array" of strings and doubles in Java?

How do I pass an array of character pointers as void *, then cast back to array of character pointers?

how to cast doubles from char array

How do I cast from a heterogeneous array in Swift 4?

How do I find the mode of an array of integers in C++ using an array to hold the counters?

How do I a split a line of integers into array?

How do I cast /copy from a char array so that I may store it in a char pointer array?

How do I parse a string of numbers into a array of integers?

How do I subtract 1 from an array of integers

How to cast a String as an Array then dedupe Array in Java?