2

I need to store 2 arrays of values that are connected to each other. The arrays consists of a set of strings and a set of integer/double values. The size of the data are not fixed.

An example:

Data 1: AA, 13    
Data 2: BB, 6    
Data 3: GG, 2

I am trying to look at 2D Arrays. Is there a better way to store the values? There's a possibilities that I might need multidimensional arrays to store the values as well. Can anyone point me in the right direction or show me a way to create the 2D arrays and how to add/retrieve the elements?

2
  • Do you need to differentiate between ints and doubles? Or can it be all pairs of String,Double ? Commented Sep 13, 2012 at 2:38
  • It's either pairs of String-double or String-integers. Sorry for the confusion. Commented Sep 13, 2012 at 2:51

2 Answers 2

7

It would be far better to use a Map. Maps have key-value pairs, so you could naturally store Strings and Integers in this manner.

Map<String, Integer> myMap = new HashMap<String, Integer>();
Sign up to request clarification or add additional context in comments.

6 Comments

Map has two type parameters, set only has one.
Ah, that's right. I must've been working with something else for far too long.
Hi Makoto, is it possible to use Map to store a set of 3 values like String-integer-integer (AA, 3, 6) or String-integer-double(KK, 6, 2.5)?
Yes - instead of storing a single Integer object, you would store a List<Integer>.
Unfortunately Makoto, Map only allows unique keys and I'm afraid my data might contain same values. It's a great learning experience nevertheless.
|
1

You can also use like this there are two ways if you want only ArrayList as a datastructure create a class like this :

    public Class Data{
    private String myString;
    private Integer myInteger;
    public String getMyString() {
        return myString;
    }

    public void setMyString(String myString) {
        this.myString = myString;
    }

    public Integer getMyInteger() {
        return myInteger;
    }

    public void setMyInteger(Integer myInteger) {
        this.myInteger = myInteger;
    }
}
List<Data> list = new ArrayList<Data>();

or else you can use like this

List<Object[]> list = new ArrayList<Object[]>();

Comments

Your Answer

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.