每当我尝试编译这个函数时,它都会在第10行给出一个错误--> ErrorMessage : CandidateCode.java.10: static HashMap hm =HashMap<>()类型的非法开始;1错误我试图在网站的编译器上编译它,但当我使用netbeans时,它工作得很好。
import java.util.*;
public class CandidateCode {
static int rep, total = 0, sum = 0, i = 0, j = 0;
static HashMap<Integer, Integer> hm = new HashMap<>();
static ArrayList<Integer> al;
public static int DistributingMedals(int input1, int[] input2, int[] input3,
int[] input4, int input5) {
//Write code here
for (i = 0; i < input1; i++) {
int start = input3[i];
int end = input4[i];
int count = input2[i];
for (j = start; j <= end; j++) {
try {
sum = hm.get(j);
} catch (Exception e) {
e.getMessage();
sum = 0;
}
sum = sum + count;
hm.put(j, sum);
}
}
int chk = 0;
Collection<Integer> valcol = hm.values();
casper:
while (chk < valcol.size()) {
for (int max : valcol) {
total = max + total;
if (total > input5) {
al = new ArrayList(hm.keySet());
Object obj = al.get(chk);
rep = (Integer) obj;
break casper;
}
chk++;
}
}
return rep;
}
}发布于 2014-09-08 22:06:31
如果您使用的是java版本1.6或更低版本,则必须具有提及的泛型类型。只有java1.7和更高版本支持<>,因此可以支持任何您可以使用的版本
HashMap hm = new HashMap<Integer, Integer>();发布于 2014-09-08 22:00:37
您的java编译器版本低于1.7。只允许在1.7+中使用<>。更改项目属性并使其使用1.7+进行编译,或者将代码更改为:
static HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>();
注意:如何检查编译器版本。
1. Right click on the project.
2. properties.
3. java compiler发布于 2014-09-08 22:02:01
将其更改为HashMap hm = new HashMap<Integer, Integer>();
https://stackoverflow.com/questions/25726016
复制相似问题