Re: swap method in Java

From: Chris Smith (cdsmith_at_twu.net)
Date: 06/23/04


Date: Tue, 22 Jun 2004 16:33:50 -0600

Stefan Ram wrote:
> When is it not applicable to swap the variable names in
> the rest of the block? I.e.,
>
> class Swap4a
> { final static int x=5, y=7 ;
> public static void main( final String[] args )
> { System.out.println( "x now is " + y + " y now is " + x ); }}

Generally speaking, when:

a) the swaps occur in a loop or conditional, or

b) the variables being swapped are not local (for example, they are
fields of an object or elements of an array), unless you can prove that
the data will only be accessed in controlled in a local environment.

c) the variables are fields of an array (even under the local exception
above), and you plan to access them in a loop. Or, on a related but
less common note, the variables are fields and you plan to access them
using reflection.

-- 
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation


Relevant Pages

  • Re: "Sorting" assignment
    ... you think using memcpy a block at a time is not the best way to swap ... trouble now or later. ... It does not bother to zero the array (you'd need a clever optimiser ... void memswap(char *ptrToA, char *ptrToB, int intLength) ...
    (comp.programming)
  • Re: qsort and arbitrary types
    ... >> or swap elements of the type pointed to. ... > an array of arbitrary type, ... > a reference to a comparison function. ...
    (comp.lang.fortran)
  • Re: vm-pageout-throttling.patch: hanging in throttle_vm_writeout/blk_congestion_wait
    ... By swapping to a loop device over a file, ... and putting the filesystem back under swap. ... Device backed loop is capable of encrypting swap on 2.4 ... File backed loops can't be used for swap. ...
    (Linux-Kernel)
  • Re: User Defined type()
    ... Swap indices when sorting, not element values. ... keep a separate array of indices and swap them around as you sort ...
    (microsoft.public.vb.general.discussion)
  • Re: Endian revisited
    ... Robert Latest skrev: ... You'd just swap the ... four bytes in your array and cast the result to the appropriate ... I cannot check to see if the binary representation on my PC and the IBM ...
    (comp.lang.c)