🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.java.help
Article #19

Re: swapping array elements

#19
From: D'Arcy Smith
Date: Sun, 29 Oct 2000 08:36
56 lines
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
Technical Details
Message-ID: <RjRK5.13280$9Z.213233@news1.crdva1.bc.home.com>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.stanford.edu!arclight.uoregon.edu!hammer.uoregon.edu!newsfeed.direct.ca!look.ca!newshub2.rdc1.sfba.home.com!news.home.com!news1.crdva1.bc.home.com.POSTED!not-for-mail
References: <8tgikk$gk6$1@bugstomper.ihug.com.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 113ae9,1141f690fb55e2e0
X-Google-Attributes: gid113ae9,public
X-Google-Thread: 10a146,1141f690fb55e2e0
X-Google-Attributes: gid10a146,public
X-Google-Thread: 10d6db,1141f690fb55e2e0
X-Google-Attributes: gid10d6db,public
X-Google-ArrivalTime: 2000-10-29 01:36:02 PST
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.stanford.edu!arclight.uoregon.edu!hammer.uoregon.edu!newsfeed.direct.ca!look.ca!newshub2.rdc1.sfba.home.com!news.home.com!news1.crdva1.bc.home.com.POSTED!not-for-mail
From: "D'Arcy Smith" <newsacct@home.com>
Newsgroups: comp.lang.java.help,comp.lang.java.programmer,aus.computers.java
References: <8tgikk$gk6$1@bugstomper.ihug.com.au>
Subject: Re: swapping array elements
Lines: 58
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4133.2400
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400
Message-ID: <RjRK5.13280$9Z.213233@news1.crdva1.bc.home.com>
Date: Sun, 29 Oct 2000 08:36:01 GMT
NNTP-Posting-Host: 24.113.67.117
X-Complaints-To: abuse@home.net
X-Trace: news1.crdva1.bc.home.com 972808561 24.113.67.117 (Sun, 29 Oct 2000 01:36:01 PDT)
NNTP-Posting-Date: Sun, 29 Oct 2000 01:36:01 PDT
Organization: Excite@Home - The Leader in Broadband