Initializing an array of pairs in Java

11,166

Solution 1

It is because of the nature of generics.

My suggestion is to drop the idea of using arrays directly, and use a List<Pair<String, Integer>> instead. Under the hood, it uses an array anyway, but a List is more flexible.

List<Pair<String, Integer>> list = new ArrayList<Pair<String, Integer>>();
// You don't have to know its size on creation, it may resize dynamically

or shorter:

List<Pair<String, Integer>> list = new ArrayList<>();

You can then retrieve its elements using list.get(index) whereas you would use list[index] with an array.

Solution 2

You can not create an array of generified type, in this case Pair. That's why your first solution works, because you did not specify the concrete type of Pair.

Technically, you can create an array, Generic arrays in Java, but it's not reccomended.

Share:
11,166

Related videos on Youtube

Matécsa Andrea
Author by

Matécsa Andrea

I am an informatics student. Coding languages: Java, JavaFX, XML, OCaml, Python, JS, C, C++, Rust, C#. Other languages: English, German, Hungarian, Romanian, Spanish. Life beyond coding: singing, playing various musical instruments, reading books, nature.

Updated on October 22, 2022

Comments

  • Matécsa Andrea
    Matécsa Andrea over 1 year

    I would like to initialize an Array of pairs but my way is not fully correct. Here how I first wrote it:

    Pair<String, Integer>[] pair = new Pair[5];
    

    It is accepted and it works but there is still the following warning:

    "Unchecked assignment: 'android.util.Pair[]' to 'android.util.Pair<Java.lang.String, Java.lang.Integer>[]'...

    I already tried to do like this:

    Pair<String, Integer>[] pair = new Pair<String, Integer>[5];
    

    but it doesn't work.

  • Matécsa Andrea
    Matécsa Andrea over 6 years
    Can you tell me than how I could solve my issue? What I do is the following: I have a lot of text to print out (I work on android) and want to give them colour. I already did it, but the amount of code is horrible and I wanted to shorten it with a function. now, for every part of my text I give a specific colour. That's why I thought about tupels, cause than I already tell for the function which part of the text it is. That's what I Need. Something to recognize easily what colour the Text should be and also Need that Information within code
  • EmberTraveller
    EmberTraveller over 6 years
    @MatécsaAndrea It depends on how you color your string, you can split it in some amount of parts and then use substring() to get parts of string at specific indexes, depending the size of your string.Then you will know what parts have what color, because it will be decided by string's size. Or your could look for specific words in string, but then again, if you maintain the same string, it should be easy to find those words again. Alternatively you can create your own class where you will store information about colored parts in some way. Sorry, can't really do much without your implementation
  • Matécsa Andrea
    Matécsa Andrea over 6 years
    I understand. Thank you. I use this now.
  • Matécsa Andrea
    Matécsa Andrea almost 6 years
    thank you @EmberTraveller it was really helpful. I almost forgot to thank you :)
  • EmberTraveller
    EmberTraveller almost 6 years
    @MatécsaAndrea it's been almost a year lol. and you did thank me. I appreciate it a lot :)
  • Washington Guedes
    Washington Guedes over 4 years
    How to add a new Pair to the list? list.add(new Pair<String, Integer>("foo", 1)) shows this error: The constructor Pair<String,Integer>(S, T) is not visible