Nisreen Rose Nisreen Rose - 12 days ago 6
Java Question

How to reverse ArrayList input?

I am working on my application Reflects Input

Example

When user enters 1, 2, 3. ArrayList will reverses and prints 3,2,1

How i can do this with ArrayList?


INPUT 1, 2, 3

OUTPUT 3, 2, 1

Answer

You can try:

ArrayList<Object> objects= new ArrayList<Object>(objects);
Collections.reverse(objects);

Or you can add them at front:

objects.add(0, yourObject);