Java – decomposes int values into different numbers
•
Java
I want to divide my int value into numbers For example, if not It's 542. The result should be 5,4,2
I have two options 1) Convert int to string & then I can have separate characters by using getchararray() & then I'll convert them to int values
2) Convert int to string instead of char array, iterate it & to get all the numbers
There are other ways to solve the problem If not, which option will be fast?
Solution
List<Integer> digits(int i) {
List<Integer> digits(int i) { List<Integer> digits = new ArrayList<Integer>(); while(i > 0) { digits.add(i % 10); i /= 10; } return digits; }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码