aspose file tools*
The moose likes Java in General and the fly likes what data structure should i use Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "what data structure should i use" Watch "what data structure should i use" New topic
Author

what data structure should i use

jessica pare
Greenhorn

Joined: Aug 28, 2006
Posts: 1
Hello All,
I am new to this forum and this is my first post. I am trying to implement a linear interpolation in Java for my numericl analysis class. I need to first populate the table which has around 40[x, y] values.

X Y
213 1331
310 1510
410 1210
475 1140
..........
...............


then based on some X values for example if X = 350 which lies between 310 and 410 take the corresponding Y values for 1510 and 1210 and find their difference. What is a good data structure that i need to use for this purpose in Java. Should i go with Arraylist, Array or map since i have around 40 such values to build the table and perform computations?


Thanks in advance
jessica
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

Look into the Point class. It already has X and Y fields and not too much extra stuff that you don't need.

It sounds like you'll be working your way up and down this table to find where 350 fits and to get the X & Y values on either side. Let me know if my guess at your algorithm is off.

A straight array of Points is probably the most efficient for that kind of thing. You could make up some kind of binary search or just step through from the first one until you find a gap you like. The worst thing about arrays is that you have to size them before you populate them. If you know how many pairs you'll have, maybe you can do that.

If not, maybe a Collection would be good. I made this little Collections Crib Sheet because I couldn't memorize all the pros and cons of each choice. See which one says "optimized for get by index".

Show us what you make. It sounds like a fun assignment.
[ August 28, 2006: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what data structure should i use