HashMap <>错误。类型非法开始 - java

每当我尝试编译此函数时,都会在第10行给出错误
->错误消息:CandidateCode.java.10:静态HashMap类型的非法启动hm = new 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;
    }
}

参考方案

如果您使用的是Java 1.6版或更低版本,则必须提及泛型类型。仅Java 1.7及更高版本支持
因此,支持您可以使用的任何版本

HashMap hm = new HashMap<Integer, Integer>();

休眠映射<键,设置<值>> - java

我有以下表格:@Entity @Table(name = "events") Event --id --name @Entity @Table(name = "state") State --id --name @Entity @Table(name = "action") Action --id …

无法从ArrayList <String>转换为List <Comparable> - java

当我写下面的代码时,编译器说 无法从ArrayList<String>转换为List<Comparable>private List<Comparable> get(){ return new ArrayList<String>(); } 但是当我用通配符编写返回类型时,代码会编译。private List&l…

合并List <T>和List <Optional <T >> - java

鉴于: List<Integer> integers = new ArrayList<>(Arrays.asList( 10, 12 )); List<Optional<Integer>> optionalIntegers = Arrays.asList( Optional.of(5), Optional.em…

在集成测试阶段执行Maven模块 - java

我想启动一个同级Maven 3模块,该模块在我的一个Maven模块中充当应用程序服务器,以对系统运行集成测试。我的maven项目看起来与此类似:父模块模块A模块B现在,我想在Maven的集成前测试阶段中启动“模块A”,然后运行模块B中包含的所有集成测试。我设法在模块B中运行了集成测试,但是没有找到“光滑”的方法在集成前测试阶段启动模块B。最佳做法是什么?使用…

当我所有的都是T时,如何返回Interface <T>的实例? - java

我有一个界面:public interface ILoginResult<T> { public T get(); } 我有一个LoginPage对象:public class LoginPage<T> { ... public ILoginResult<T> login(...) { ... } } 我也有一些登录页面对…