2

I need to store few data pairs in array. Maybe few dozens. I only need to append, no need to delete, no need to search. Then I will access by index. Each pair is String value and Integer value. Java provides so many way to do this, which is the common practice for something like that? Two arrays? A class in an array?

I know how to do this in JavaScript:

var data = []
data.push(['Some name', 100])
//somewhere else
data.push(['Other name', 200])

but I need a solution for Java

Thank you.

asked Jun 4, 2013 at 9:20
6
  • 1
    java or javascript? :) Commented Jun 4, 2013 at 9:21
  • Do you really need an array rather than a list? I'd just create a class to encapsulate that pair of presumably-related values, and then create a list of that type, e.g. List<Foo> foos = new ArrayList<Foo>(); Commented Jun 4, 2013 at 9:22
  • Java indeed. I use JavaScript as example because I know how to do it in JavaScript and have do not know what is the way in Java. Commented Jun 4, 2013 at 9:23
  • Creating an object with an attribute of a String and an Integer, at least for me, is the most convenient especially when it comes to maintainability. Commented Jun 4, 2013 at 9:23
  • you know Map ? docs.oracle.com/javase/6/docs/api/java/util/Map.html, which you can use hashMap, multimap as you want Commented Jun 4, 2013 at 9:25

3 Answers 3

4

For example you can create Pair class (or use implementations from apache commons) to store two elements in List.

List<Pair<String, Integer>> l = new ArrayList<>();
l.add(new Pair<String, Integer>("Some name", 100));

See Generic pair class and Java Pair<T,N> class implementation to see how you can implement Pair class.

answered Jun 4, 2013 at 9:25

Comments

3

It really depends, but in general I think it is better to create an object and use a list of it:

public class MyObject {
 private String myString;
 private Integer myInt;
 // getters setters
}

And use:

List<MyObject> = new ArrayList<>();

(you can also use Pair instead)

If the strings (or ints) are unique, you can use Map, but it is harder to get the insert index.

Another option is just two lists, one for Strings, one for Integers, and use same index in both lists.

answered Jun 4, 2013 at 9:23

Comments

0

I go by using POJO as suggested above for this as this helps to define getter and setter for all the attributes of POJO, compare the objects by overriding equals and hashCode methods. By using getter and setter you know what is stored in what field and comparison can provide you sorting of objects as per your requirements. So this approach is cleaner and extensible for accommodating new requirements too. Also as you are putting data with sequential key so each instance of Pojo can be put in List (if required in sorted order.

answered Jun 4, 2013 at 9:45

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.