JAVA“插入维度以完成Expression/ReferenceType”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34885463/
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
JAVA "insert Dimensions to complete Expression/ReferenceType"
提问by meesinlid
I'm a newbie to Java.
我是 Java 的新手。
I have provided a short snippet from my code for BFS.
我从我的 BFS 代码中提供了一个简短的片段。
public int bfs(Person p, Person q) {
private HashMap<Person, boolean> marked;
private int count;
marked = new marked<Person, boolean>();
count = new int;
}
According to Eclipse, I have an error on each of the last 4 lines.
根据 Eclipse,我在最后 4 行中的每一行都有错误。
Syntax Error: insert "Dimensions" to complete expression/referencetype.
语法错误:插入“维度”以完成表达式/引用类型。
I would appreciate any input/advice!
我将不胜感激任何输入/建议!
采纳答案by Satyendra Sharma
Cause of this error -You are trying to pass a primitive object into a generic type declaration whereas generic types always expect a Wrapper Class object. So please use 'Boolean' instead of 'boolean' in your code i.e. 'B' in caps.
此错误的原因 - 您正在尝试将原始对象传递到泛型类型声明中,而泛型类型始终需要包装类对象。所以请在您的代码中使用“Boolean”而不是“boolean”,即大写字母“B”。
回答by Clauds
First I would suggest you start reading a Java tutorial...
首先,我建议您开始阅读 Java 教程...
https://docs.oracle.com/javase/tutorial/java/TOC.html
https://docs.oracle.com/javase/tutorial/java/TOC.html
For your issues specifically:
针对您的问题:
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
- https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
As for your code, you can initialize your variables right when you declare them:
至于您的代码,您可以在声明变量时立即对其进行初始化:
Map<Person, Boolean> marked = new HashMap<Person, Boolean>();
int count = 0; // or whatever initial value
回答by bcsb1001
It seems that this snippet is throwing around random keywords without any understanding - I would suggest a Java tutorial. First of all, generics are one of the main uses for boxing. boolean
or any other primitives (you can recognise these by the fact that their identifiers are in lower-case and most IDEs will highlight them) cannotbe used as a generic type, and their capitalised equivalent must be used (a simple wrapper class). Here, use HashMap<Person, Boolean>
.
似乎这个片段在没有任何理解的情况下抛出随机关键字 - 我建议使用 Java 教程。首先,泛型是拳击的主要用途之一。boolean
或任何其他原语(您可以通过它们的标识符以小写形式识别这些原语,并且大多数 IDE 会突出显示它们)不能用作泛型类型,并且必须使用它们的大写等价物(一个简单的包装类)。在这里,使用HashMap<Person, Boolean>
.
I'm not sure what is meant by marked = new marked...
- clearly, marked
is not a type and cannot be used in this context. new x(params)
initialises an object of type x
, passing its constructor params
. new x<generics>(params)
is the same but the generic type(s) of x
are generics
.
我不确定是什么意思marked = new marked...
- 显然,marked
它不是一种类型,不能在这种情况下使用。new x(params)
初始化一个类型的对象x
,传递它的构造函数params
。new x<generics>(params)
是相同的,但泛型类型x
是generics
。
Finally, new int
is not at all valid - see my explanation above. Primitives are notobjects, which means initialising them is meaningless and therefore invalid. Also, what do you expect this expression to yield? Something of type int
, but you are not specifying which int
. The correct syntax is a literal: count = x;
where x
is some integer within the range of int
.
最后,new int
根本无效 - 请参阅我上面的解释。原语不是对象,这意味着初始化它们是没有意义的,因此是无效的。另外,你希望这个表达式产生什么?某种类型的东西int
,但您没有指定哪个int
. 正确的语法是文字:count = x;
wherex
是范围内的某个整数int
。
As a side note, your method has an unclear name and variables may be initialised in the same line you declare them to simplify code.
作为旁注,您的方法名称不明确,并且变量可能在您声明它们的同一行中初始化以简化代码。
回答by Rishi Raj Tandon
You need to use the wrapper object not the primitive. Use Boolean instead of boolean.
您需要使用包装器对象而不是原语。使用布尔值而不是布尔值。
回答by Soumyajit Swain
Generic are resolved during compile time and during runtime their no context about the generic used in your code. The Object is than type cast into the class type provided against the generic type. Now both primitive and object are completely unrelated entities in java. Direct time-cast of Object to primitive type isn't possible in java. For this reason the use of primitive type in generic is disallowed and eclipse gives this warning.
泛型在编译时和运行时解析,它们没有关于代码中使用的泛型的上下文。然后将 Object 类型转换为针对泛型类型提供的类类型。现在原始和对象在java中都是完全不相关的实体。在 Java 中不可能将 Object 直接时间转换为原始类型。出于这个原因,不允许在泛型中使用原始类型,并且 Eclipse 会发出此警告。
回答by lzhlyle
Visit Cannot Instantiate Generic Types with Primitive Types
Replace all type parameters in generic types with their bounds or Object if the type parameters are unbounded. The produced bytecode, therefore, contains only ordinary classes, interfaces, and methods.
如果类型参数是无界的,则将泛型类型中的所有类型参数替换为其边界或对象。因此,生成的字节码只包含普通的类、接口和方法。
The type parameter, V
, actually also K
, which is declared in HashMap<K,V>
, will be replaced with Object
after erasing, because they are unbounded. While primitive type can not be store as Object
.
在 中声明的类型参数 ,V
实际上也是K
,擦除HashMap<K,V>
后将被替换为Object
,因为它们是无界的。虽然原始类型不能存储为Object
.