1

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.

exebook
  • 32,014
  • 33
  • 141
  • 226
  • 1
    java or javascript? :) – Subhrajyoti Majumder Jun 04 '13 at 09: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 foos = new ArrayList();` – Jon Skeet Jun 04 '13 at 09: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. – exebook Jun 04 '13 at 09: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. – Michael 'Maik' Ardan Jun 04 '13 at 09:23
  • you know Map ? http://docs.oracle.com/javase/6/docs/api/java/util/Map.html, which you can use hashMap, multimap as you want – dinesh707 Jun 04 '13 at 09:25
  • Sorry, I used the word "array" because in Russia we used to call anything that has 2+ items an "array". Actually if that's a list or a map or whatever else is ok. Anything that I can access by integer ID and can add dynamycally. – exebook Jun 04 '13 at 09:26

3 Answers3

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.

Community
  • 1
  • 1
4ndrew
  • 15,354
  • 2
  • 27
  • 29
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.

Community
  • 1
  • 1
BobTheBuilder
  • 18,858
  • 6
  • 40
  • 61
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.

Harish Kumar
  • 528
  • 2
  • 15