12 Replies Latest reply: Oct 30, 2006 4:26 PM by 807598 RSS

    "swap" arrays

    807598
      I'm having some trouble. I need to have the program swap the first array with the last.

      char[] table = {'a', 'b', 'c', 'd'};

      table.swap(a,d);

      Without the swap, it displays a, b, c, d and i can't figure out how to make the program swap the a and d so it will display d, b, c, a. The problem is in the swap code because i don't know how to do what i want it to do.
        • 1. Re: "swap" arrays
          807598
          you do this:
          make another array of the same size,
          then get the last element of your first array and stick it into the first element of the new array
          then loop over elements 1 through length -1 of the first array and copy each element at current index into the new array
          then copy the first element of the first array into the last element of the new array.
          • 2. Re: "swap" arrays
            3004
            Or just swap them in the original array. It depends on which one meets your requirements.

            Do you know how to get and set the values of array elements?
            http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html


            Do you know how to swap two variables' values? HINT: Use a thirs, "temporary" variable.


            Take your best shot, and then post your code with details about what problems you're having.

            When you post code, please use[code] and [/code] tags as described in Formatting tips on the message entry page. It makes it much easier to read.
            • 3. Re: "swap" arrays
              807598
              You don't need a second array.

              Copy first element into a temp variable.
              Copy last element into first element.
              Copy temp variable into last element.
              • 4. Re: "swap" arrays
                807598
                oh screw you guys, i like my 2 array solution!
                what if he needs the original array for some other branch of processing, didnt think of that did you!

                Message was edited by:
                mkoryak
                • 5. Re: "swap" arrays
                  3004
                  oh screw you guys, i like my 2 array solution!
                  what if he needs the original array for some other
                  branch of processing, didnt think of that did you!
                  I did. I would assume flounder did as well. He's not as dumb as he looks.
                  • 6. Re: "swap" arrays
                    807598
                    yea, that's the easy man approach. of course i could change it to
                    char[] table = {'d', 'b', 'c', 'a'};
                    but i do NOT want to change this line. I want to add a new line, possibly one line of code that would change the 'a' array with the 'd' array. I'm looking through the Java documentaion but no luck.

                    edit: isn't there actually a swap code?
                    • 7. Re: "swap" arrays
                      3004
                      yea, that's the easy man approach. of course i could
                      change it to
                      char[] table = {'d', 'b', 'c', 'a'};
                      That's not what we meant. Both approaches mentioned here are general algorithms that can be used to swap any two elements of any array.


                      edit: isn't there actually a swap code?
                      Probably not. But it's 3 lines of code to write your own.
                      • 8. Re: "swap" arrays
                        807598
                        could you tell me exactly what these 3 lines are. i'm so lost.
                        • 9. Re: "swap" arrays
                          807598
                          oh screw you guys, i like my 2 array solution!
                          what if he needs the original array for some other
                          branch of processing, didnt think of that did you!
                          I did. I would assume flounder did as well. He's not
                          as dumb as he looks.
                          Hey! Is that an insult or a compliment?
                          • 10. Re: "swap" arrays
                            807598
                            See reply #3.
                            • 11. Re: "swap" arrays
                              807598
                                  public static void main(String[] args) throws Exception
                                  {
                                      char[] chars =
                                      { 'a', 'b', 'c', 'd' };
                                      swap( chars, 0, 3 );
                                      System.out.println( chars );
                                  }
                              
                              
                                  public static void swap(char[] chars, int position1, int position2)
                                  {
                                      char temp = chars[position1];
                                      chars[position1] = chars[position2];
                                      chars[position2] = temp;
                                  }
                              • 12. Re: "swap" arrays
                                807598
                                http://www.fluffycat.com/Java/Arrays/