How to reverse ArrayList elements in java?
Simply calling Collections.reverse() method you can reverse elements of the ArrayList. You have to pass the list object which you need to swap.
package com.tech.mindclues; import java.util.ArrayList; import java.util.Collections; public class ReverseArrayList { public static void main(String args[]) { ArrayListreverselist = new ArrayList (); reverselist.add("Mind"); reverselist.add("Clues"); reverselist.add("Hunt"); reverselist.add("Hagrid"); reverselist.add("Bond"); reverselist.add("Tom"); Collections.reverse(reverselist); System.out.println("ArrayList Elements are reversed :"); for (String result : reverselist) { System.out.println(result); } } }
ArrayList Elements are reverse order : Tom Bond Hagrid Hunt Clues Mind
post a comment