The compiler complains about this code:
HashMap<String,int> userName2ind = new HashMap<String,int>();
for (int i=0; i<=players.length; i++) {
userName2ind.put(orderedUserNames[i],i+1);
}
It writes “unexpected type” and point on int. If I replace int by String and i+1 by i+"1", the compilation goes OK. What is wrong with in here?
It’s fine with
Integer, but not okay withint– Java generics only work with reference types, basically 🙁Try this – although be aware it will box everything: