简体   繁体   中英

Generic type as method parameter

I have an assignment that I'm having a little trouble with. The following method headers are required, and I can not change them. I have to provide an implementation of the following method:

contains2(T anEntry)
{
//my code here
}

that calls a private recursive method:

private boolean contains(T anEntry, Node startNode)
{
//my code here
}

My problem is, how to I get the input of type T from method contains2, and make it so method contains takes that same type T parameter? This is what I've tried.

private boolean contains2(T anEntry)
{
boolean found = false;
T entry = T anEntry;

Node startingNode = firstNode;

if (contains(entry, startingNode) = true)
found = true;

return found;

}//end method contains2

private boolean contains(T anEntry, Node startNode)
{
boolean found = false;
return found;
}//end method contains

I get the following error "required: variable, found: value" for the "entry" parameter for this line of code:

if (contains(entry, startingNode) = true)

Please ignore that my code isn't complete for the assignment, right now I just need that line of code to accept type T as input.

Your problem is right here:

T entry = T anEntry;

This is invalid syntax. Setting aside for the moment the uselessness of declaring an extra reference to the input parameter and comparing a boolean value to true , you should simply omit the second T from that line.

Here's your other problem:

if(contains(entry, startingNode) = true)

Here you are trying to assign the value true to an expression. You want the == operator. Or just don't compare a boolean to true as that doesn't change the result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM