Reverse the order of elements in an array

how would i go about Reverse the order of elements in an array??

I link to the javadocs can't be too bad. [url
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Coll
>
ections.html#reverse(java.util.List)]Collections.rever
se(List)Of course I fortell the next question, but I wantan
Array not a List? So I provide the next link:[url
http://java.sun.com/j2se/1.5.0/docs/api/java/util/Arr
ys.html#asList(T...)]Arrays.asList(T...)Yeah but then you have to worry about the next
question which is more of a statement: "I can't use
Arrays".
Then I would as them: How would you do it in real life? Of course they could think a real world way of doing it. In that case, I'll give them an real life example.
Assumed List
1 2 3 4 5 6 7 8 9 10 11 12 13
Switch the first and the last elements
13 2 3 4 5 6 7 8 9 10 11 12 1
Then the second and second to the last
13 12 3 4 5 6 7 8 9 10 11 2 1
And repeat until your half way.
13 12 10 4 5 6 7 8 9 3 2 1
13 12 10 9 5 6 7 8 4 3 2 1
13 12 10 9 8 6 7 5 4 3 2 1
13 12 10 9 8 7 6 5 4 3 2 1
Done.

Similar Messages

  • Reverse the order of elements in array?

    Hi All,
    i have elements of string[] aray = {one,two,three};i want the elements needs to be in reverse order..
    ex : {three,two,one};how can we do this...
    plz help on this...with example..
    thanks in advance..
    jags.

    Melanie_Green wrote:
    Eric-Fistons wrote:
    int j = 0;
    for (int i = array.length - 1; i >= 0; i--) {
    newArray[j] = array;
    j++;
    Simplified
    int j = 0;
    for (int i = array.length; i > 0; i--){
    newArray[j++] = array;
    }Aka bored, Mel
    package forums;
    import java.util.Arrays;
    class ArrayReverserator
      public static void main(String[] args) {
        try {
          System.out.println(Arrays.toString(reverse(new Object[]{1,2,3})));
        } catch (Exception e) {
          e.printStackTrace();
      public static Object[] reverse(Object[] a) {
        Object[] result = new Object[a.length];
        for ( int i=a.length-1,j=0; i>=0; --i ) {
          result[j++] = a;
    return result;
    I also am bored...
    <tut-tut>for i=a.length; i>0; == you're slipping</tut-tut>
    IMHO, java.util.Arrays could stand to have a reverse... it is, after all, just special case of sort... by index decending.
    Cheers. Keith.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • The order of elements in an array.

    Hi
    I have an array whose array.length is 3.
    I need to add a new element at the the first position in the array.
    For example the existing values in the array are
    1,2,3.
    Now I need to add 10 in the first position so that the array looks like : 10,1,2,3.
    Is there any Java API that accomplishes this.
    Any help is highly appreciated.
    Thanks in advance.

    You can use the new appendToBeginning method of the CrazyMadeUp Class. It's available in version 1.6 (codename Monkey) and above. Unfortunately it only works with Objects though.
    public static Object[] appendToBeginning(Object[] originalArray, Object[] addToStart) {
      Object[] tempArray = new Object[originalArray.length + addToStart.length];
      System.arraycopy(originalArray, 0, tempArray, addToStart.length, originalArray.length);
      for (int i = 0; i < addToStart.length; i++) tempArray[i] = addToStart;
    return tempArray;

  • How can i change the order of  elements in a JList ?

    Hi,
    i would like to know if it is possible to change the order of elements in a JList ? Maybe i could use drag and drop to do this ?
    Thanks for answering.

    The simplest way is probably to implement ur own listmodel and then supply the listitems in the order u want. Example:
    class MyListModel
              extends AbstractListModel
              implements ContactListListener {
    public int getSize() {
    return listitemcount;
    public Object getElementAt(int index) {
    //here u return the item for the index at the list
    //if u for example have an array of strings
    //which u wanna have listed in reversed order (reversed to
    //the order in the array, u could do
         return myarray[getSize()-index];
    }

  • How to reverse the order of an OrderedMap?

    Is there a quick and efficient way to reverse the order of an OrderedMap?
    I'm sorta using an OrderedMap for 'breadcrumbs'. Traversing from a specific page, category by category all the way up to the top.
    For example:
    OrderedMap map = new LinkedMap();
    map.put( "Rabbit", "" );
    map.put( "Mammals", "/animals/mammals/index.jsp" );
    map.put( "Animals", "/animals/index.jsp" );
    Iterating it in my JSP view page, I get this order, which is wrong :
    Home >> Rabbit >> Mammals >> Animals
    Thats why I was wondering how I can reverse the order of the OrderedMap to get this:
    Home >> Animals >> Mammals >> Rabbit
    Thanks for reading.
    Regards,
    Stanley

    What about the following idea:
    you could write two wrapper classes:
    class ReverseOrderedMap implements OrderedMapand
    class ReverseOrderedMapIterator  implements OrderedMapIteratorIn these classes, you just map the methods
    firstKey -> lastKey
    lastKey -> firstKey
    nextKey -> previousKey
    next -> previous
    and so on. In this way, you just provide a "reversed view" on the original map.
    However, I'm not sure where elements added with put are ordered: Is it just in the sequence of addition? If yes, then the wrapped map behaves wrong, if new elements are added, because there is no corresponding "put at the begining" method. In this case, the wrapper should make the reversed map unmodifyable by implementing the put and putAll methods with throwing an exception.

  • How to reverse the order of a list (nodes)

    i can't figure out how to reverse the order of the values in a list of nodes. Could anyone help?
    public class IntList
         public IntNode head;
         public IntList()
              head = null;
         public void reverse()
              // I don't know how to write this
    }

    read through the list from the end to the start, putting each element into a new list. then replace the original list with the new one.
    lots of ways it can be done.
    dont ask me which is more efficient. the stack suggestion sounds pretty good.

  • Can I reverse the order of finder lists or icons?

    Hello,
    I am finding some problems with Lion that I never seemed to have with Snow Leopard and would very much appreciate someone's help.
    To cut a long story short I want to reverse the order of the files in finder, either in list or icon view (or maybe any kind of view). At the moment they are in date created order, but I have also tried in name order and cannot see any way to reverse the order.
    At present the are show the newest file first but I want to see them in oldest first as they always were for me in OSX 10.6
    Also... in date created view they appear in groups (eg previous 7 days, previous 30 days) I have never seen this before and find it particulalry annoying. I'm sure some people love it but for me it is one of the things that I most hated about windows, but at least in Windows I found it easy to turn off (along with reversing order!). Can anyone tell me how to stop OSX from doing this?
    Much obliged,
    Pablo

    Welcome to Apple Support Communities. We're users here and don't speak for "Apple, Inc."
    In Finder icon view, click the Arrange button and select which column you'd like to sort by.
    In Finder list view, click the top header of any column to sort by that column. Clicking the same header again reverses the sort order (and the small 'arrow' at the end of the column name reverses direction too).
    And using Finder, View, Customize Toolbar the Finder window toolbar can be customized to display icons only, text only or both, as well as adding additional icons to the default toolbar:
    Message was edited by: kostby

  • How do I reverse the order of photos/years in osx

    with the new OS X photos software it shows the most recent year at the bottom of my screen. How do I reverse the order so the most recent year is at the top. Thank you

    You can't at this time. Send a feature request to Apple.  You can use this feedback form:
    Apple - Mac OS X - Feedback

  • How can I reverse the order of podcasts so I can listen in cronological order

    How can I reverse the order of podcasts so I can listen in cronological order!

    What I did was create a playlist for the podcasts and keep it sorted in reverse date order, which puts the oldest first. To prevent it from automatically playing the next podcast, I actually mark all podcasts as "skip when shuffling", and then play in shuffle mode. I also make sure all my podcasts have a genre of "podcast"; I discovered that if you use media type, the playlist doesn't update correctly on the iPod classic
    So, what I've got is a playlist of the form: (play count = 0) and (genre = podcast), sorted in reverse date order.

  • Reverse the order of Legend in Column Chart

    Hello All,
    Hope all is well. I tried to find way of doing it .... I am currently getting my legends in order ... but I wanted to reverse it ...can it be done ?
    I can put simple text as legend but probolem is I have show/Hide series on and hence it wouldn't work ?
    Anyone ?
    Thanks in advance,
    Sam

    Hi,
    Since you are looking after reversing the Legends Only..
    I.g your column Order is A,B,C  With their correspodingin values in the cells A1:C2     
    Just change the order Map C1->D1, B1->D2, A1 -> D3 and with the values as well...
    This is only way if you want to reverse the order of legends..You can set any properites in the column char or if that matter any chart .There is no such property..
    I hope this helps......
    If you find that is done thru some other ways please do let me know...

  • How to reverse the order of a bunch of still images in an iMovie Project

    Hello,
    I'm making a stop motion animation using iMovie'09. I need to reverse the order of a few hundred of the images that I imported.
    Does anybody know a good and time efficient way of doing this?
    Cheers,
    Onur.

    Ok, here's how have I done:
    1. Removed those images from the movie project
    2. Created a new project and added all the images which I want to reverse the order of
    3. Set the durations of the images as same as those in my first project
    4. Exported the project as a movie file
    5. Added this short movie file to the first project
    6. From clip adjustments menu, checked 'reverse'.
    There, the subset of the whole collection is displayed in the reverse order.
    Message was edited by: Onur

  • Will the order of elements stored in pl.sql table retains

    Hello Friends,
    I am having a record type and the for each element of record , i am having corresponding pl.sql table type .
    If i am storing the values into the records from a query and also the individual elements in the pl.sql table type will the order of data is stored as it is . ..
    example...
    in a record type the data is stored as ( name1 , age1 , salary1) , (name2, age2, salary2)
    if i store in corresponding pl sql table type name1 , name2
    age1, age2
    salary1, salary2
    can i relate the index of record type with that of pl/sql table type ..
    pls advice
    thanks/kumar

    Kumar,
    Yes, the order of elements will be the same.
    Any specific reason why you would want to create a collection for each individual attribute of the record ?
    You can as well declare another variable of the same record and initialize it.
    A few other suggestions for your code :
    1) The 2nd FOR loop can be modified to accommodate the query of the first cursor there-by eliminating one extra iteration.
    SELECT MINC.FAMID,
                         MINC.MEMBNO,
                         MEMB.AGE,
                         SALARYX,
                         SALARYBX,
                         NONFARMX,
                         NONFRMBX,
                         FARMINCX,
                         FRMINCBX,
                         CU_CODE
                    FROM MINC, MEMB, FMLY
                   WHERE MINC.FAMID = MEMB.FAMID
                     AND MINC.MEMBNO = MEMB.MEMBNO
                     AND MINC.FAMID = FMLY.FAMID
                     AND MEMB.FAMID = FMLY.FAMID
                   ORDER BY MINC.FAMID2) The collections can be alternately initialized as follows :
       v_member_rec(v_member_rec.last).FAMID := j.FAMID;
       max_earnings_tab(max_earnings_tab.last) := v_max_earnings;The tried the below example for confirmation ...
    declare
    type emp_rec is record
    (name emp.ename%TYPE,
      dept emp.edept%TYPE,
      sal  emp.esal%TYPE
    TYPE emp_rec_tab is table of emp_rec;
    ert emp_rec_tab := emp_rec_tab();
    TYPE ename_tab is table of varchar2(20);
    ent  ename_tab := ename_tab();
    TYPE edept_tab is table of number;
    edt  edept_tab := edept_tab();
    begin
    for i in (select * from emp)
    loop
         ert.extend;
         ent.extend;
         edt.extend;
         ert(ert.last).name := i.ename;
         ert(ert.last).dept := i.edept;
         ert(ert.last).sal := i.esal;
         ent(ent.last) := i.ename;
         edt(edt.last) := i.edept;
    end loop;
    for i in 1..ert.count
    loop
         dbms_output.put_line(ert(i).name||','||ent(i));
         dbms_output.put_line(ert(i).dept||','||edt(i));
         dbms_output.put_line('');
    end loop;
    end;

  • Reverse the order of stacks list view

    Is there a way to reverse the order when viewing the contents of a stack in list view by date added?
    For example, the downloads stack: the default setting puts the most recently added at the top of the list, meaning I have to scroll up to it, rather than at the bottom, where it'd be right there above the icon to click on.
    Thanks for the help

    I completely agree with Solar Servant,
    The regular stacks view is viewable in this (very practical) direction, so why not the list variety?
    thanks for making a more usable OSX a reality

  • Can't remember how to reverse the order of the tracks in a playlist ~ can anyone help  me out?  Thanks.

    Can't remember how to reverse the order of tracks in a playlist.....can anyone help me out?  Thanks.

    Were you able to figure it out?

  • Is there a way to reverse the order of incoming mail in the new iPad?

    I want to reverse the order of incoming mail on my new iPad.    I want the oldest on top.  I can't seem to find where or even if I can do this.  Anyone know for sure?  Thanks!

    The Mail app only supports having the most recent at the top. You could see if any third-party email apps support it, or access your account via a browser (you can create a Safari shortcut on your iPad's homescreen for your login page).

Maybe you are looking for

  • Pacman -Qm no longer working correctly [solved]

    I can't get pacman -Qm to work right anymore, when I use it, it just displays pretty much every package I have, like so: $ pacman -Qm acl 2.2.39-1 alsa-lib 1.0.13-1 alsa-oss 1.0.12-1 alsa-utils 1.0.13-1 apache 2.2.4-2 apr 1.2.8-1 apr-util 1.2.8-2 asp

  • Imac told me to restart and now will not boot up?

    Hi, I have an imac 20" intel (not the new aluminium one but the white version) which I have had for a few months. Today i was using it and all was normal until I had a grey swoosh thing come doen over the screen from top to bottom and then a black bo

  • Gr/ir mb5s

    hi everyone mb5s give gr/ir balances as now i would like to devp a zee program for this can anyone explane me steps thanks

  • Question de securité / Security question

    Bonjour.Afin de pouvoir effectuer mes achats itunes sur un nouvel ordianteur itunes me demande (comme c'est la premiere fois que je veux acheter sur un autre ordinateur ) de repondre a mes questions de securitée.Le probleme est que cela fait longtemp

  • EJB underneath Web Services

    hi there, For each EJB entity, i have to make a Model (implements Serializable) but, if an EJB entity has a CMR field (Collection or Set), I can't make a Web service using a model containing a Collection or a Set attribut ... How can I do ? Replace t