How do I store a set of paired numbers in java? Do I use lists or arrays or maybe something else?
eg. [ (1,1) , (2,1) , (3,5)]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a few options:
Write a custom IntPair class
and then create an
IntPair[]or aList<IntPair>.Alternately, create a two-dimensional array
new int[n][2], and treat the rows as pairs.Java doesn’t have a built-in
Pairclass for a few reasons, but the most noticeable is that it’s easy enough to write a class that has the same function, but has much more enlightening, helpful names for the class, its fields, and its methods.If we knew more about what you’re actually using this for, we might be able to provide more detailed suggestions — for all we know, a
Mapcould be appropriate here.