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.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

Similar Messages

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

  • 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).

  • Will Vector keep the order of elements?

    I would like to quickly add lines of a file into a vector to get the size. I then create an array using that declaring that number of elements, then read each vector element into the array. I thought I read somewhere that a Vector may swap the order of the elements?

    I thought I read
    somewhere that a Vector may swap the order of the
    elements?Well, if you did read that somewhere it might be true. If you write some serious code you cannot buy what people just say. You have to look into it for yourself and ESTABLISH what goes with your chosen data-structure.
    PS. My advice to everybody is to look into this stuff and convince yourself you're doing the right thing.

Maybe you are looking for

  • Problem in downloading and installing adobe apps in windows phone.

    How to download adobe apps. eg, adobe flash player in my windows phone Nokia Lumia 720? I tried several times but failed to download & installed, as showed my device does not support it! But I downloaded adobe reader from windows store, but did not f

  • Sleep problem with iMacs

    I am happy owner of 2.8 GHz Intel iMac except with one nagging problem. When I leave my computer in the SLEEP mode for more than 24 hours it tends to not respond and WAKE when I return to use it. I have to unplug power and restart. Since I usually us

  • Creating search help for AFNAM, and display the hit list with values only

    Hi guys, I have created a search help for AFNAM, but the hit list displays all even if it is blank. I want to display in the hit list the AFNAM with values and do not diplay the blank values... Is it possible? How am i going to do it? Thanks! Mark

  • SystemTray and Swing question

    Not sure if this should go in AWT or here, but here goes. I have implemented system tray functionality in my program, and for all purposes, it works fine. However, it's a bit ugly, and doesn't have all the functionality I want it to have when the men

  • Best  compressor settings for web videos shot on canon 7d?

    Hello, I've started shooting video on the Canon 7d.  I usually compress my videos for youtube as MP4 at 300 Kbps.  However my last video had a presenter with background music track added.  When I did my first export to compressor without the music ba