简体   繁体   English

Java - 反转 ArrayList 或 List of objects 的单一通用方法

[英]Java - a single generic method to reverse ArrayList or List of objects

I have seen that there is a very convenient way to reverse ArrayList like here using Collections.reverse() method, but I need to achieve such a reverse operation for several types of primitive objects, like at least ArrayList and List of my custom objects.我已经看到有扭转一个非常方便的方法ArrayList喜欢这里使用Collections.reverse()方法,但我需要实现这样的反向操作数的基本类型的对象,比如至少ArrayList ,并List我的自定义对象。 So Collections can't save my life for an extended usage of a single method.因此,对于单一方法的扩展使用, Collections无法挽救我的生命。

So far I managed to get stg working (See code below) but I have to write it twice: once for ArrayList and once for List.到目前为止,我设法让 stg 工作(见下面的代码),但我必须写两次:一次用于 ArrayList,一次用于 List。 I named them both the same so usage is very convenient, but some .. optimization would be even more convenient :-).我将它们命名为相同的,因此使用起来非常方便,但是一些 .. 优化会更方便:-)。

How to commonalize these 2 methods into a single (real) generic one ?如何将这 2 种方法通用化为一个(真正的)通用方法?

Thx谢谢


/**
 * Generic Reverse Method for ARRAYLIST using Iterator
 * @param ArrayList<Obj_item> notReversed
 * @return ArrayList<Obj_item> reversed
 */
@SuppressWarnings("unchecked")
public static <Obj_item> ArrayList<Obj_item> GenReverseArrayByIterator(ArrayList<Obj_item> mylist) {
                       // ^^^^^^^^^^^^^^^^^^ how to make this generic ?
    // Instanciate
    ArrayList<Obj_item> tmp = new ArrayList<Obj_item>();
    ArrayList<Obj_item> reversed = new ArrayList<Obj_item>();

    // Copy original list
    tmp = mylist;

    // Generate an iterator, starting right after the last element.
    @SuppressWarnings("rawtypes")
    ListIterator myIterator = tmp.listIterator(tmp.size());

    // Iterate in reverse direction
    while (myIterator .hasPrevious()) {
        reversed.add((Obj_item) myIterator.previous());
    }
    return reversed;
}

Collections.reverse takes a List<?> . Collections.reverse采用List<?> ArrayList implements List - you can use that method on an ArrayList or any List implementation. ArrayList实现List - 您可以在ArrayList或任何List实现上使用该方法。 What you're doing seems entirely unnecessary, unless I misunderstood the question.你在做什么似乎完全没有必要,除非我误解了这个问题。

Collections.reverse() reverses any instance of List . Collections.reverse()反转List任何实例。 So yes, you can use Collections.reverse() on anArrayList , or any instance of List .所以是的,您可以在ArrayListList任何实例上使用Collections.reverse()

But anyway, here's how'd you write it by hand if you wanted:但无论如何,如果您愿意,可以通过以下方式手写:

static void reverse(List<?> list) {
  for (int i = 0, l = list.size() / 2, j = list.size() - 1; i < l; i++, j--) {
    Object tmp = list.get(i);
    list.set(i, list.get(j));
    list.set(j, tmp);  
  }
}

Below generic method should reverse any type of List or ArrayList下面的通用方法应该反转任何类型的 List 或 ArrayList

public static <T> List<T> reverseList(List<T> listToReverse){
    List<T> reverseList = new ArrayList<>(listToReverse);
    Collections.reverse(reverseList);
    return reverseList;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM