Insert Dimensions to complete Expression/ReferenceType

后端 未结 7 1807
醉话见心
醉话见心 2021-01-03 21:55

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

        
相关标签:
7条回答
  • 2021-01-03 22:37

    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.

    0 讨论(0)
  • 2021-01-03 22:43

    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
    
    0 讨论(0)
  • 2021-01-03 22:44

    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.

    0 讨论(0)
  • 2021-01-03 22:50

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

    0 讨论(0)
  • 2021-01-03 22:52

    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.

    0 讨论(0)
  • 2021-01-03 22:55

    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.

    0 讨论(0)
提交回复
热议问题