Article View: aus.computers.java
Article #850Re: Trouble accessing values in a 2D array
From: Paul Murray
Date: Sat, 13 May 2000 00:00
Date: Sat, 13 May 2000 00:00
58 lines
2810 bytes
2810 bytes
"Tim Cross" <tcross@northnet.com.au> wrote in message
news:m3aehwgmco.fsf@tim.northnet.com.au...
> "Paul Murray" <pmurray@nospam.bigpond.com> writes:
> [snip]
>
> > On a more general note, you might be better off using the Collections
API
> > rather than explicitly putting things in arrays. If you do that, you may
as
> > well use a class for a floating-point XY coord. This allows you to
attach
> > methods etc. java.util has integer Point and Dimension classes - why not
> > model your floating-point classes on those?
> >
> I think Paul is spot on here with this advice. Create your own classes
> to represent your coordinates and this will give you much better data
> encapsulation. As Paul points out, it will also allow you to associate
> methods with this data. For example, you might be able to add a
> distance method to your coord class which, when passed another coord
> object returns the distance/metric between the two points etc. There
> is also less chance of your system becomming inconsistant which could
> happen if one of your arrays is updated and the other isn't etc.
>
Totally
public class Coord implements Serializable {
public final double x, y; // a co-ord is an "Immutable" class
public Coord(double x, double y) { this.x=x; this.y = y; }
public boolean equals(Object o) {
if(!o instanceof Coord)
return false;
return x == ((Coord)o).x && y == ((Coord)o).y;
}
public int hashvalue() {
long l;
l = Double.doubleToLongBits(x) ^ Double.doubleToLongBits(y);
return (int)(l ^ (l>>32));
}
public double distFrom(Coord c) {
return Math.sqrt((x-c.x)*(x-c.x) + (y-c.y)*(y-c.y));
}
}
--
--------------------------------------------------------------
http://www.users.bigpond.com/pmurray
ICQ: 26066755
> --
> Tim Cross
> NorthNet Internet Services
> mailto:tcross@northnet.com.au
> phone:+61 2 6772 5973
Technical Details
Message-ID:
<Hz6T4.10158$X4.21136@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<39189457.3527647@news.melbpc.org.au> <8fbd5b$kbd$1@nnrp1.deja.com> <K3uS4.8857$X4.17863@newsfeeds.bigpond.com> <m3aehwgmco.fsf@tim.northnet.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,1002517bab792639 X-Google-Attributes: gid10d6db,public From: "Paul Murray" <pmurray@nospam.bigpond.com> Subject: Re: Trouble accessing values in a 2D array Date: 2000/05/13 Message-ID: <Hz6T4.10158$X4.21136@newsfeeds.bigpond.com> X-Deja-AN: 622850443 References: <39189457.3527647@news.melbpc.org.au> <8fbd5b$kbd$1@nnrp1.deja.com> <K3uS4.8857$X4.17863@newsfeeds.bigpond.com> <m3aehwgmco.fsf@tim.northnet.com.au> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 X-Trace: newsfeeds.bigpond.com 958199143 139.134.123.54 (Sat, 13 May 2000 16:25:43 EST) Organization: Telstra BigPond Internet Services (http://www.bigpond.com) X-MSMail-Priority: Normal NNTP-Posting-Date: Sat, 13 May 2000 16:25:43 EST Newsgroups: aus.computers.java