I have this simple Pair class that I have made, and whenever I call the main method I get an error message saying that: "Exception in thread "main" java.lang.Error: Unresolved compilation problem: The constructor Pair<String,Integer>(String, int) is undefined"
Which is weird as I have defined the consturctor.
public class Pair<K, V> {
K key;
V value;
public void set(K k, V v) {
key = k;
value = v;
}
public K getKey() { return key; }
public V getValue() { return value; }
public String toString() {
return "[" getKey() ", " getValue() "]";
}
public static <T> void reverse(Pair<T,T> p) {
p.set(p.getValue(), p.getKey());
}
public static void main (String[] args) {
Pair<String, Integer> pairA = new Pair<String, Integer>("abc",1);
}
}
CodePudding user response:
You are missing a constructor:
public Pair(K k, V v) { ... }
Or you should do
Pair<String, Integer> pairA = new Pair<>();
pairA.set("abc", 1);
Notice you can use the "diamond" operator <> saving typing.
CodePudding user response:
You need to use an Integer instead of an int in the construction of pairA, e.g. Pair<String, Integer> pairA = new Pair<String, Integer>("abc",new Integer(1));
