Quantcast
Channel: Collections.reverse method not reversing entry - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Answer by 1218985 for Collections.reverse method not reversing entry

$
0
0

The reverse(List<?>) method is used to reverse the order of the elements in the specified list. Since there is only one item in your collection, reverse order is same as the initial list. You can use the below code as you are trying to reverse an integer.

package com.stackoverflow.answer;import java.util.*;public class ReverseDigits {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.print("Please enter a whole number: ");        int number = scanner.nextInt();        System.out.println(String.format("Your number in reverse order is: %d", reverse(number)));        scanner.close();    }    public static int reverse(int x) {        return reverse(x, 0);    }    private static int reverse(int x, int y) {        return x == 0 ? y : reverse(x / 10, y * 10 + x % 10);    }}

Viewing all articles
Browse latest Browse all 6

Trending Articles