Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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 (46k 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);

Related questions

0 votes
1 answer
0 votes
1 answer
asked Aug 6, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...