Here you can find the source of reverseCopy(List
Parameter | Description |
---|---|
original | the list to revert |
public static <E> List<E> reverseCopy(List<E> original)
//package com.java2s; /*/* w ww . ja va 2 s .c o m*/ * Copyright 2014 Stathis Aliprantis * * This program is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published * by the Free Software Foundation, either version 3 of the License or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with This program. If not, see http://www.gnu.org/licenses/. */ import java.util.*; public class Main { /** * Returns a reversed shallow copy of the given list. * The result is {@link Serializable} and {@link RandomAccess}. * * @param original the list to revert * @return a reversed shallow copy of the given list */ public static <E> List<E> reverseCopy(List<E> original) { List<E> result = new ArrayList<E>(original.size()); if (original instanceof RandomAccess) { for (int i = original.size() - 1; i >= 0; i--) { result.add(original.get(i)); } } else { ListIterator<E> it = original.listIterator(original.size()); while (it.hasPrevious()) { result.add(it.previous()); } } return result; } }