• Post Reply Bookmark Topic Watch Topic
  • New Topic

what data structure should i use  RSS feed

jessica pare
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!