"swap" arrays
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.
[398 byte] By [
Agent_Fa] at [2007-10-3 8:38:00]

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.
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 [url=http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
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.
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
> 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.
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?
> 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.
could you tell me exactly what these 3 lines are. i'm so lost.
> > 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?
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;
}
http://www.fluffycat.com/Java/Arrays/