Thread View: comp.lang.java.help
4 messages
4 total messages
Started by John Fu
Sun, 29 Oct 2000 07:13
swapping array elements
Author: John Fu
Date: Sun, 29 Oct 2000 07:13
Date: Sun, 29 Oct 2000 07:13
28 lines
2079 bytes
2079 bytes
I have a problem with my code. And I find that my swap method isn't really
swapping the elements declared in my arrays. Can someone help please. I
know it's just something simple that I'm missing, but I just can't catch it.
Thanks.
import java.io.*;
public class test
{
static int[] pos = new int[3];
public static void main(String args[]) throws Exception
{
pos[0] = 1;
pos[1] = 2;
pos[2] = 3;
swap(pos[0],pos[1]);
System.out.println("pos[0] = "+pos[0]);
System.out.println("pos[1] = "+pos[1]);
System.out.println("pos[2] = "+pos[2]);
}//end main
public static void swap(int x, int y) throws Exception
{
int tmp;
tmp = x;
x = y;
y = tmp;
}//end swap
}
Re: swapping array elements
Author: D'Arcy Smith
Date: Sun, 29 Oct 2000 08:36
Date: Sun, 29 Oct 2000 08:36
56 lines
2952 bytes
2952 bytes
"John Fu" <zenki@ihug.com.au> wrote in message
news:8tgikk$gk6$1@bugstomper.ihug.com.au...
> I have a problem with my code. And I find that my swap method isn't really
> swapping the elements declared in my arrays. Can someone help please. I
> know it's just something simple that I'm missing, but I just can't catch
it.
> Thanks.
> public class test
> {
> static int[] pos = new int[3];
> public static void main(String args[]) throws Exception
> {
> pos[0] = 1;
> pos[1] = 2;
> pos[2] = 3;
> swap(pos[0],pos[1]);
> System.out.println("pos[0] = "+pos[0]);
> System.out.println("pos[1] = "+pos[1]);
> System.out.println("pos[2] = "+pos[2]);
> }//end main
> public static void swap(int x, int y) throws Exception
> {
> int tmp;
> tmp = x;
> x = y;
> y = tmp;
> }//end swap
> }
Primitive values in Java are pass by value - so all you are doing is
swapping the
local variables x and y.
References (Objects) are done by passing the reference by value so what
you want to do will not work there either.
You could write a swap methods like:
public static void swap(int[] array, int src, int dst)
{
int tmp;
tmp = array[src];
array[src] = array[dst];
array[dst] = tmp;
}
src and dst are probably bad names... but you get the idea I hope.
Why are you declaring that it throws "Exception"? It isn't needed
and it is generaly a bad idea to throw/catch "Exception" - you should
be dealing with the appropriate subclass.
..darcy
Re: swapping array elements
Author: Deepak Grewal
Date: Mon, 13 Nov 2000 00:00
Date: Mon, 13 Nov 2000 00:00
41 lines
2214 bytes
2214 bytes
hi
i m kinda new to java so maybe this isnt the best advice but u can still
try....
when u call the method swap.... shouldnt u be saying this.swap
instead......??
as the swap method is in the same class.............
tell me if it did or didnt work........
John Fu <zenki@ihug.com.au> wrote in message
news:8tgikk$gk6$1@bugstomper.ihug.com.au...
> I have a problem with my code. And I find that my swap method isn't really
> swapping the elements declared in my arrays. Can someone help please. I
> know it's just something simple that I'm missing, but I just can't catch
it.
> Thanks.
>
> import java.io.*;
>
> public class test
> {
> static int[] pos = new int[3];
> public static void main(String args[]) throws Exception
> {
> pos[0] = 1;
> pos[1] = 2;
> pos[2] = 3;
> swap(pos[0],pos[1]);
> System.out.println("pos[0] = "+pos[0]);
> System.out.println("pos[1] = "+pos[1]);
> System.out.println("pos[2] = "+pos[2]);
> }//end main
> public static void swap(int x, int y) throws Exception
> {
> int tmp;
> tmp = x;
> x = y;
> y = tmp;
> }//end swap
> }
>
>
Re: swapping array elements
Author: Jon Skeet
Date: Mon, 13 Nov 2000 00:00
Date: Mon, 13 Nov 2000 00:00
13 lines
1257 bytes
1257 bytes
deepakgrewal@rediffmail.com wrote: > i m kinda new to java so maybe this isnt the best advice but u can still > try.... > when u call the method swap.... shouldnt u be saying this.swap > instead......?? > as the swap method is in the same class............. > tell me if it did or didnt work........ No, that wouldn't work. this.swap() and swap() are synonymous. -- Jon Skeet - skeet@pobox.com http://www.pobox.com/~skeet
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads