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 5 of 5

Thread: How to rearrange an array?

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

    Default How to rearrange an array?

    Hey guys, in my journey through java i ran in to a bit of a problem.

    How do i rearrange a array?

    // Enter values to test here 
        String[] s = { "from", "first", "to", "last" }; 
        String w; 
    System.out.println(w);
        // Enter your code here

    How would i rearrange array s, to compile backwards in string w? so it would read "last to first from"?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: How to rearrange an array?

    If all you need is all the elements of the array in reverse order then you can use a reverse loop. A normal loop would start at the first element and iterate until it reaches the last element. A reverse loop starts at the last element and iterates until it reaches the first element.

    On the other hand, if you want to move elements about then you will need to copy element A into a temp variable, copy element B into the position element A was, copy the temp variable into the position element B was.
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    41
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to rearrange an array?

    Since you are "Compiling" into a string, you can just use a reverse loop, like Junky said, and then add the elements one at a time to w. Don't forget to add spaces!

  4. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: How to rearrange an array?

    Quote Originally Posted by jjd712 View Post
    How do i rearrange a array?
    Pseudo-code for one possible solution to reverse any array:
    //
    // Reverse elements of an array in-place.
    // No extra memory required.
    // Single loop goes through half of the array.
    //
     
    Define int variables i and j
     
    Set i equal to 0, so it is indexing the first element of the array.
     
    Set j equal to array.length-1, so that it is indexing the last element of the array.
     
    Repeat the following loop as long as i is less than j
    BEGIN LOOP
       Swap array[i] and array[j];
       Increment i;
       Decrement j;
    END LOOP

    Cheers!

    Z
    Last edited by Zaphod_b; October 17th, 2012 at 11:54 AM.

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

    Default Re: How to rearrange an array?

    Thanks to everyone that helped. Worked fine!

Similar Threads

  1. How do I compare between the array of char and array of words !!?
    By bady2050 in forum Collections and Generics
    Replies: 1
    Last Post: May 5th, 2012, 05:36 PM
  2. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  3. Doubling The Array Size And Randomizing Array Return
    By Pingu00 in forum What's Wrong With My Code?
    Replies: 18
    Last Post: June 27th, 2011, 10:50 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Replies: 2
    Last Post: May 6th, 2011, 05:19 PM