的HashMap<>错误。类型非法开始 [英] HashMap<> error. Illegal start of type

查看:173
本文介绍了的HashMap<>错误。类型非法开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我尝试编译这个函数时,它会在第10行出现错误
- > ErrorMessage:CandidateCode.java.10:非法启动类型静态HashMap hm = new HashMap<>(); 1错误
我试图在一个网站的编译器上编译它,但是当我使用netbeans时它工作得很好。

Whenever i try to compile this function it gives an error at line 10 --> ErrorMessage : CandidateCode.java.10: illegal start of type static HashMap hm = new HashMap<>(); 1 error I'm trying to compile it on an website's compiler, but when i use netbeans it works completely fine.

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及以上版本支持<>
因此,您可以使用任何版本

If you are using java version 1.6 or below version you must have mention generic types. Only java 1.7 and upwards support <> Therefore to support any version you can use

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

这篇关于的HashMap&LT;&GT;错误。类型非法开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆