What is the Simplest Way to Reverse an ArrayList?

Ishu picture Ishu · May 26, 2012 · Viewed 322.2k times · Source

What is the simplest way to reverse this ArrayList?

ArrayList<Integer> aList = new ArrayList<>();

//Add elements to ArrayList object
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");

while (aList.listIterator().hasPrevious())
  Log.d("reverse", "" + aList.listIterator().previous());

Answer

Shankar Agarwal picture Shankar Agarwal · May 26, 2012
Collections.reverse(aList);

Example (Reference):

ArrayList aList = new ArrayList();
//Add elements to ArrayList object
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
Collections.reverse(aList);
System.out.println("After Reverse Order, ArrayList Contains : " + aList);