Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;TreeMap不接受参数“;编译错误_Java_Compiler Errors_Treemap - Fatal编程技术网

Java &引用;TreeMap不接受参数“;编译错误

Java &引用;TreeMap不接受参数“;编译错误,java,compiler-errors,treemap,Java,Compiler Errors,Treemap,这是我的代码: import java.util.*; public class TreeMap { public static void main(String[] args) { Map<String,Integer> treemap = new TreeMap<String,Integer>(); Some code to fill the treemap ie treemap.put("Kevin", 36); }

这是我的代码:

import java.util.*;
public class TreeMap {
    public static void main(String[] args) {
        Map<String,Integer> treemap = new TreeMap<String,Integer>();
        Some code to fill the treemap ie treemap.put("Kevin", 36);
    }
}
import java.util.*;
公共类树映射{
公共静态void main(字符串[]args){
Map treemap=新treemap();
一些代码来填充树映射,即treemap.put(“Kevin”,36);
}
}
我得到了这个编译器错误:

TreeMap.java:5: error: type TreeMap does not take parameters
   Map<String,Integer> treemap = new TreeMap<String,Integer>();
                                            ^
TreeMap.java:5:错误:类型TreeMap不接受参数
Map treemap=newtreemap();
^
重命名你的类。它与
java.util.TreeMap


编译器认为
new TreeMap()
引用了您自己的类,它不接受任何参数。

为Java TreeMap类使用完全限定的类名

new java.util.TreeMap<String,Integer>()
newjava.util.TreeMap()

为了澄清Eran的评论,导入java.util.*导入java.util中的所有内容,其中包括java.util.TreeMap和一些您没有使用的东西。这通常不是一个好的编程实践,因为这意味着有更多的名称可能与您选择使用的名称发生冲突(如上面发生的情况)。以后不再导入java.util.*而是尝试只导入您实际需要的对象。

没有帮助,我将其重命名为TreeMapTest,并且得到了相同的错误;除了公共类TestMapTree{}和import java.util.TreeMap()之外;您是否将该文件重命名为TestMapTree.java(在其中定义了该类),并且,看起来您更改了import语句,请将import语句保留为
import java.util.TreeMap是的。解决我问题的方法是:Map treemap=newjava.util.treemap();另外,我意识到我必须使用静态类型SortedMap才能访问firstKey和lastKey方法。@user3302053如果导入该类,则无需使用完整的类名。我收到另一个编译错误:TestMapTree.java:1:error:';'exptected它指的是我的导入行,当然还有;一开始我误解了你的评论。但这确实解决了我的问题,谢谢!为什么会发生这种情况?下面的代码对我来说很好:import java.util.*;公共类TreeMap{publicstaticvoidmain(String[]args){Map TreeMap=newjava.util.TreeMap();}}是的。非常感谢。
new java.util.TreeMap<String,Integer>()