How to reverse an array - Java Language Basics

Java examples for Language Basics:Array

Description

How to reverse an array

Demo Code


import java.util.Collections;
import java.util.List;
import java.util.Arrays;
 
public class Main {
       /*w w w.  j  av a2  s .c om*/
        public static void main(String args[]){
                String[] strDays = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday"};
                List<String> list = Arrays.asList(strDays);
               
                Collections.reverse(list);
               
                strDays = (String[]) list.toArray();
               
                System.out.println("String array reversed");
               
                for(int i=0; i < strDays.length; i++){
                        System.out.println(strDays[i]);
                }
               
        }
 
}

Result


Related Tutorials