This week's book giveaway is in the JDBC forum.
We're giving away four copies of Make it so: Java DB Connections & Transactions and have Marcho Behler on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes what data structure is appropriate for me? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what data structure is appropriate for me?" Watch "what data structure is appropriate for me?" New topic

what data structure is appropriate for me?

James Byars
Ranch Hand

Joined: Apr 15, 2009
Posts: 42
hi all,

i am writing a program where i will have a list of two objects, the first will be a string, and the second an integer.


list = ((string1, int1), (string2, int2) ... (stringN, intN))

i then have to go through the list of these two objects, look at intX, do some calculation on it, and depending on the result, put (stringX, intX) it into another list.

can someone please suggest to me what data structure would be best to use for (stringN, intN). I am just thinking of simply having a Vector for each (stringN, intN) i.e. have a list of Vectors, but maybe there is a better way?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Kurt Van Etten
Ranch Hand

Joined: Sep 07, 2010
Posts: 98
You could create a little class consisting of just the two data members. That might have a little less overhead than using one of the container classes. (Then again, maybe not. I've never tried comparing the two approaches to see if there's a measurable difference, but it might make an interesting experiment.)
marc weber

Joined: Aug 31, 2004
Posts: 11343

As Seetharaman suggested, paired values sounds like a Map -- but only if one of your elements (either the String or the int) can be used as a unique key for the pair. If that's not the case, then Kurt's suggestion of a special class makes sense.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
go for Map buddy

Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4272

If you end up using tuples, you should probably use ArrayList. Vector is old and tired.

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
It is sorta covered in the JavaRanch Style Guide.
subject: what data structure is appropriate for me?
jQuery in Action, 3rd edition