Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Dimensions to complete Expression/ReferenceType

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!

like image 554
meesinlid Avatar asked Jan 19 '16 19:01

meesinlid


4 Answers

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.

like image 71
Satyendra Sharma Avatar answered Nov 16 '22 12:11

Satyendra Sharma


You need to use the wrapper object not the primitive. Use Boolean instead of boolean.

like image 45
Rishi Raj Tandon Avatar answered Nov 16 '22 12:11

Rishi Raj Tandon


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.

like image 4
MC Emperor Avatar answered Nov 16 '22 14:11

MC Emperor


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.

like image 1
Soumyajit Swain Avatar answered Nov 16 '22 14:11

Soumyajit Swain