2

I get ArrayList:

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

How I can get this list in reverse order?

8
  • 1
    see Guava Commented Feb 20, 2013 at 19:09
  • 1
    Use LinkedList and call addFirst..if you wanna use ArrayList then you can call insert(element, 0) Commented Feb 20, 2013 at 19:10
  • 3
    @Ovidiu: Or the JDK. Commented Feb 20, 2013 at 19:10
  • 2
    Can't you just reverse the list later on using Collections.reverse? Commented Feb 20, 2013 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. Commented Feb 20, 2013 at 19:11

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@ruakh True, I musunderstood the code actually. Now it's done.
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.

Comments

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()
    ;

4 Comments

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.
+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.
If the interface was List and not ArrayList, using get(i) could spell a performance disaster larger than reverse. We have listIterator.
OP used ArrayList. Using LinkedList I would have given the descendingIterator code instead. You raise a good point though.
2

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

 Collections.reverse(logs);

Comments

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();) {
  ...
}

Comments

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);
        }

    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.