0

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. So Collections can't save my life for an extended usage of a single method.

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. 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 ?

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;
}
Community
  • 1
  • 1
hornetbzz
  • 9,188
  • 5
  • 36
  • 53
  • 4
    Your method already *is* generic - and it's still not clear why you can't use `Collections.reverse()`. Also, your use of the word "primitive" sounds wrong to me... – Jon Skeet Sep 12 '12 at 23:11
  • Sorry for the wrong terminology usage, as for me, "primitive" reads as "primitive objects" like ArrayList, opposit to custom objects. The point is that I have to write this method twice: once for ArrayList and once for List. I would like to write a single method if possible. – hornetbzz Sep 12 '12 at 23:23
  • 3
    You mean library classes - there's no such thing as a primitive object! – Michael Berry Sep 12 '12 at 23:23
  • 3
    @hornetbzz: No, you really don't - because of Paul's answer. It's also worth becoming aware of Java terminology and using it consistently - the primitive types are `int`, `short`, `boolean` etc. – Jon Skeet Sep 12 '12 at 23:25
  • I tried to use Collections but that did not do the trick as I have several "library classes" to get sorted or reversed. Depending on the custom object they content, it works or not but could not get it fixed after hours, therefore I wrote those 2 generic methods, which work for all my needs. – hornetbzz Sep 12 '12 at 23:31

3 Answers3

8

Collections.reverse takes a List<?>. ArrayList implements List - you can use that method on an ArrayList or any List implementation. What you're doing seems entirely unnecessary, unless I misunderstood the question.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
  • Thx. No no, you have perfectly well understood the question. Your answer makes thgs more cristal clear for me as a noob. Thx again to you both guys. – hornetbzz Sep 12 '12 at 23:32
  • Make sure your custom object implements Compareable for this to work correctly. – Bill Sep 12 '12 at 23:41
  • 3
    @Bill That shouldn't matter as the objects aren't being sorted - just reversed in order. – Paul Bellora Sep 13 '12 at 01:29
4

Collections.reverse() reverses any instance of List. So yes, you can use Collections.reverse() on an ArrayList, or any instance of List.

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);  
  }
}
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
0

Below generic method should reverse any type of List or ArrayList

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