问题
I'm a newbie to Java.
I have provided a short snippet from my code for 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.
Syntax Error: insert "Dimensions" to complete expression/referencetype.
I would appreciate any input/advice!
回答1:
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.
回答2:
You need to use the wrapper object not the primitive. Use Boolean instead of boolean.
回答3:
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.
回答4:
First I would suggest you start reading a Java tutorial...
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
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
回答5:
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) cannot be used as a generic type, and their capitalised equivalent must be used (a simple wrapper class). Here, use 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
.
Finally, new int
is not at all valid - see my explanation above. Primitives are not objects, 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
.
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.
回答6:
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
.
回答7:
Satyendra Sharma's answer is absolutely correct, but here's some reasoning of what exactly the error message is saying.
The error is caused by using a primitive type, which cannot be used as a generic type argument. For instance, List<boolean>
is incorrect, whereas List<Boolean>
is correct. Wrapper classes can be used to wrap the primitive values and yield a reference type, which can be used with generics.
Insert dimensions? What?
The message "Insert dimensions to complete expression/referenceType" is probably because in order for the expression to become valid, the only valid token here is a set of square brackets.
For instance,
HashMap<Person, boolean[]> marked;
will just compile fine. This is because, unlike a boolean
, a boolean[] is an object.
来源:https://stackoverflow.com/questions/34885463/insert-dimensions-to-complete-expression-referencetype