Reverse an ArrayList

import java.util.ArrayList;
import java.util.List;

/**
 * Java Program to reverse ArrayList in place
 *
 */
public class ReverseArrayList 
{
    public static void main( String[] args )
    {
        List<String> listOfFood  = new ArrayList<String>();
        listOfFood.add("Beans");
        listOfFood.add("Soup");
        listOfFood.add("Dark Chocolate");
        listOfFood.add("Yogurt");
        listOfFood.add("Sausage");
        listOfFood.add("Pure Vegetables");
        listOfFood.add("Nuts");
        
        System.out.println("Origibal Array : "+ listOfFood);
        
        int size = listOfFood.size();
        
        for(int i=0 ; i< size/2 ; i++){
        	String tmp = listOfFood.get(i);
        	listOfFood.set(i, listOfFood.get(size-i-1));
        	listOfFood.set(size-i-1, tmp);
        }
        
        System.out.println("Reversed ArrayList : "+ listOfFood);
    }
}
Advertisements

About sivateja

I am a professional graduate..I am passionate about Latest Technology.
This entry was posted in Programing questions and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s