Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How convert an int into an Array number by number

I have one number, for example "1256", how can I convert it into an Array?

Actually, I use a constructor of class where I stock it.

public SecretBlock(int numbersToArray) {
    this.arrayOfNumbers = new int[AMOUNT];
    for (int i = AMOUNT - 1; i >= 0; i--) {
        this.arrayOfNumbers[i] = numbersToArray % 10;
        numbersToArray /= 10;
    }
}

Is there any fine/ adequate solution that may use Java 8 Stream?

like image 222
Artem Avatar asked Jun 01 '18 09:06

Artem


People also ask

How do you convert int to number?

just use constructor of Number class.

How do you create an array of integers?

To initialize or instantiate an array as we declare it, meaning we assign values as when we create the array, we can use the following shorthand syntax: int[] myArray = {13, 14, 15}; Or, you could generate a stream of values and assign it back to the array: int[] intArray = IntStream.


1 Answers

 int[] result = String.valueOf(numbersToArray)
            .chars()
            .map(Character::getNumericValue)
            .toArray();
like image 86
Eugene Avatar answered Sep 27 '22 23:09

Eugene