Initializing an array of pairs in Java
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.
Related videos on Youtube
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, 2022Comments
-
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.
-
Malte Hartwig over 6 yearsPossible duplicate of What's the reason I can't create generic array types in Java?
-
-
Matécsa Andrea over 6 yearsCan 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 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 over 6 yearsI understand. Thank you. I use this now.
-
Matécsa Andrea almost 6 yearsthank you @EmberTraveller it was really helpful. I almost forgot to thank you :)
-
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 over 4 yearsHow 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