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!
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.
You need to use the wrapper object not the primitive. Use Boolean instead of boolean.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With