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?
just use constructor of Number class.
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.
int[] result = String.valueOf(numbersToArray)
.chars()
.map(Character::getNumericValue)
.toArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With