Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How do I reverse an ArrayList ?

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I reverse an ArrayList ?

    I am trying to figure out how to write a method that will reverse an ArrayList. I can't use collections.reverse(). I'm lost, my teacher speaks broken English so I'm basically on my own here. I think that I should use a for loop, something like this:

    for (int index = arrayList.size(); index = 0; index--)

    But, I don't understand how to make it return the reversed arrayList. Any help is appreciated.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: How do I reverse an ArrayList ?

    You could do it in a loop. Take a piece of paper and a pencil, draw some objects on a row representing the objects in the arraylist. Use indexes/pointers (one at each end) and work out the logic for reversing the contents.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reverse a String
    By Mehwish-S in forum What's Wrong With My Code?
    Replies: 4
    Last Post: August 22nd, 2012, 09:03 AM
  2. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java SE API Tutorials
    Replies: 4
    Last Post: December 21st, 2011, 04:44 AM
  3. Reverse String
    By WantHelp in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 5th, 2011, 06:14 AM
  4. Reverse Number
    By java1 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 28th, 2009, 10:19 AM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM