Java:在 Map、HashMap 上找不到符号错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7863727/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:45:23  来源:igfitidea点击:

Java: Cannot find symbol error on Map, HashMap

javamissing-symbols

提问by Lasse A Karlsen

I'm trying to run this code:

我正在尝试运行此代码:

import java.util.*;

public class ScanReg {
  public Map<Integer, ArrayList<Long>> scanMap = new HashMap<Integer, ArrayList<Long>>();
}

within this class:

在这个类中:

import java.util.*;

public class NxtStart {
  ScanReg sr = new ScanReg();
}

This keeps giving me the following error:

这不断给我以下错误:

.\ScanReg.java:6: error: cannot find symbol
        public Map<Integer, ArrayList<Long>> scanMap = new HashMap<Integer, Arra
yList<Long>>();
               ^
  symbol:   class Map
  location: class ScanReg
.\ScanReg.java:6: error: cannot find symbol
        public Map<Integer, ArrayList<Long>> scanMap = new HashMap<Integer, Arra
yList<Long>>();
                                                           ^
  symbol:   class HashMap
  location: class ScanReg
2 errors

Can somebody please tell me why?

有人可以告诉我为什么吗?

回答by Kal

You're possibly compiling using Java 1.4 and using generics ( only available from 1.5 onwards ).

您可能正在使用 Java 1.4 进行编译并使用泛型(仅从 1.5 开始可用)。

回答by Edward Aung

You need to declare your inner class as static

您需要将内部类声明为静态

public static class ScanReg {}

Else, put in different java file and import ScanReg.

否则,放入不同的java文件并导入ScanReg。