0 votes
1 view
in Java by (3.4k points)

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());

1 Answer

0 votes
by (26.9k points)

Try:

Collections.reverse(aList);

For instance (check this):

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);

...