I wrote some code to reverse a number as below:
long num = 123456789;
char[] arr = String.valueOf(num).toCharArray();
List<Character> characterList = new ArrayList<Character>();
for (char c : arr) {
characterList.add(c);
}
Collections.reverse(characterList);
for (Character c : characterList) {
System.out.println(c);
}
output:
9
8
7
6
5
4
3
2
1
Could anyone advise me a more efficient way to achieve this with Java?
EDIT:
In fact, the first guide about this question is to print them backwards, please just ignore this way.
while(num != 0) { println(num % 10); num /= 10; }