Java List Reverse reverse(List aList)

Here you can find the source of reverse(List aList)

Description

Reverses the items in the given list.

License

Open Source License

Declaration

public static List reverse(List aList) 

Method Source Code


//package com.java2s;

import java.util.*;

public class Main {
    /**//from   w  ww  . j a  v  a  2s  .  c  o  m
     * Reverses the items in the given list.
     */
    public static List reverse(List aList) {
        for (int i = 0, max = aList.size(), iMax = max / 2; i < iMax; i++) {
            int oppositeIndex = max - i - 1;
            Object original = aList.set(i, aList.get(oppositeIndex));
            aList.set(oppositeIndex, original);
        }

        return aList;
    }

    /**
     * Returns the size of a list (accepts null list).
     */
    public static int size(List aList) {
        return aList == null ? 0 : aList.size();
    }

    /**
     * Returns the object at the given index (returns null object for null list or invalid index).
     */
    public static <T> T get(List<T> aList, int anIndex) {
        return aList == null || anIndex < 0 || anIndex >= aList.size() ? null : aList.get(anIndex);
    }
}

Related

  1. reverse(final List list)
  2. reverse(final List list)
  3. reverse(final List list)
  4. reverse(final List list)
  5. reverse(final List strings)
  6. reverse(List list)
  7. reverse(List list)
  8. reverse(List as)
  9. reverse(List list)