2

I get ArrayList:

ArrayList logs;
for(Logs log : getLogs()){           
    logs.add(log.getName();        
}

How I can get this list in reverse order?

koopajah
  • 23,792
  • 9
  • 78
  • 104
Jffs2ej
  • 109
  • 2
  • 3
  • 11
  • 1
    see [Guava](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/Lists.html#reverse(java.util.List)) – yohlulz Feb 20 '13 at 19:09
  • 1
    Use `LinkedList` and call `addFirst`..if you wanna use `ArrayList` then you can call `insert(element, 0)` – Shivam Feb 20 '13 at 19:10
  • I tried get it through count of objects. But I think, that it is a more easy way – Jffs2ej Feb 20 '13 at 19:10
  • 3
    @Ovidiu: Or [the JDK](http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#reverse%28java.util.List%29). – ruakh Feb 20 '13 at 19:10
  • 2
    Can't you just reverse the list later on using `Collections.reverse`? – Rohit Jain Feb 20 '13 at 19:11
  • 1
    @Jffs2ej: If you have working code, then you should post it. That will make it easier for others to post improved code that preserves your code's behavior. – ruakh Feb 20 '13 at 19:11
  • Push the elements on stack and Just Pop it... You're done ! – Parth Soni Feb 20 '13 at 19:11
  • http://stackoverflow.com/questions/1098117/can-one-do-a-for-each-loop-in-java-in-reverse-order – zeddarn Feb 20 '13 at 19:13

6 Answers6

6

Use the reverse method of the Collections class to reverse the list. Please note that this will affect the list itself.

ArrayList logs = new ArrayList(); //Initialize this list or your
                                  //code won't compile.

for(Logs log : getLogs()){           
    logs.add(log.getName();        
}

Collections.reverse(logs);

If you want a new list, create a new one, add all of the elements to it by using addAll and, finally, reverse that one.

Fritz
  • 9,987
  • 4
  • 30
  • 49
5

Try using add(int index, E element).

ArrayList logs;
for(Logs log : getLogs()){           
    logs.add(0,log.getName());        
}

This will always insert the element as the first element instead of appending it to the end of the list so your list is reversed.

Danny
  • 7,368
  • 8
  • 46
  • 70
5
for ( int i = logs.size() - 1; i >= 0; --i )
    // do something with logs.get( i )
    ;

Don't waste time reversing something you can iterate in reverse order.

UPDATE: listIterator can also be used in reverse order, and is a more general solution:

for ( ListIterator< Logs > lit = logs.listIterator( logs.size() ); lit.hasPrevious(); )
    // do something with lit.previous()
    ;
Judge Mental
  • 5,209
  • 17
  • 22
  • This is a good point. You would want to make a concious decision on weather or not to sort based on how you intend to use the list. If you just want to read once in reverse.. you should do this. – gbtimmon Feb 20 '13 at 19:17
  • +1 for thinking outside the box. Tough the OP asked about getting the list in the reverse order, not the elements, this one is still a valid option. I also agree with @gbtimmon that for a simple "read backwards" case this is the way to go. – Fritz Feb 20 '13 at 19:17
  • 2
    If the interface was `List` and not `ArrayList`, using `get(i)` could spell a performance disaster larger than `reverse`. We have `listIterator`. – Marko Topolnik Feb 20 '13 at 19:21
  • OP used ArrayList. Using LinkedList I would have given the descendingIterator code instead. You raise a good point though. – Judge Mental Feb 21 '13 at 02:27
2

As of Java 5 (at least, maybe sooner), there is support for this in the Collections library.

 Collections.reverse(logs);
gbtimmon
  • 4,238
  • 1
  • 21
  • 36
0

Reversing the collection is not necessary and is potentially a performance disaster (depending on the size). The most natural JDK way is to involve a listIterator:

for (ListIterator<String> it = list.listIterator(list.size()); it.hasPrevious();) {
  ...
}
Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
0
/** ArrayList Elements Reverse Without Using Collections Reverse */
public static void main(String s[]) {

        ArrayList<Integer> alOrig = new ArrayList<Integer>();
        alOrig.add(210);
        alOrig.add(213);
        alOrig.add(214);
        alOrig.add(216);
        alOrig.add(217);

        System.out.println("ArrayList Elements in Normal Order");
        Iterator alOrigItr = alOrig.iterator();

        while (alOrigItr.hasNext()) {
            System.out.println("" + alOrigItr.next());
        }

        System.out.println("ArrayList Elements in Reverse Order ");

        ArrayList<Integer> alRev = new ArrayList<Integer>();

        for (int i = alOrig.size(); i > 0; i--) {
            alRev.add(alOrig.size() - i, alOrig.get(i - 1));
        }

        for (Integer alRevI : alRev) {

            System.out.println("" + alRevI);
        }

    }

}
ArunKR
  • 3
  • 2