首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HashMap<>错误。非法的类型开始

问HashMap<>错误。非法的类型开始
EN

Stack Overflow用户
提问于 2014-09-08 21:59:18
回答 3查看 9.2K关注 0票数 2

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

代码语言:javascript
复制
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;
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-08 22:06:31

如果您使用的是java版本1.6或更低版本,则必须具有提及的泛型类型。只有java1.7和更高版本支持<>,因此可以支持任何您可以使用的版本

代码语言:javascript
复制
HashMap hm = new HashMap<Integer, Integer>();
票数 4
EN

Stack Overflow用户

发布于 2014-09-08 22:00:37

您的java编译器版本低于1.7。只允许在1.7+中使用<>。更改项目属性并使其使用1.7+进行编译,或者将代码更改为:

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

注意:如何检查编译器版本。

代码语言:javascript
复制
1. Right click on the project.
2. properties.
3. java compiler
票数 6
EN

Stack Overflow用户

发布于 2014-09-08 22:02:01

将其更改为HashMap hm = new HashMap<Integer, Integer>();

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25726016

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档