[英]Need help in java generics parameter List <T>
Hi guys i have a problem with this code: 嗨,我对此代码有疑问:
//Class FileManip
public class FileManip{
private HTTPRequest<List<String>> addMe = new HTTPRequest<List<String>>();
public void openFile(){
try{
BufferedReader buff = new BufferedReader(new FileReader("Saturday.txt"));
try{
Double ctr = 0.0;
String readHTTPURIFromTxt = buff.readLine();
while (readHTTPURIFromTxt!=null){
if (!readHTTPURIFromTxt.isEmpty()){
addMe.putListHTTPCharTable(parseHTTPReqToCharbyChar(readHTTPURIFromTxt), ctr);
ctr++;
}
readHTTPURIFromTxt = buff.readLine();
}
}
finally{
buff.close();
}
}
catch(FileNotFoundException e){
System.out.println("File not found"+e);
}
catch(IOException e){
System.out.println("oist exception");
}
}
public List<String> parseHTTPReqToCharbyChar(String getHTTP){
List<String> parsingReq = new ArrayList<String>();
String convChar=null;
for (int x = 0; x<getHTTP.length(); x++){
convChar = Character.toString(getHTTP.charAt(x));
parsingReq.add(convChar);
}
return parsingReq;
}
}
//Class HTTPRequest
public class HTTPRequest<T> {
private LinkedHashMap<List<T>, Double> tableOfInitProbList = new LinkedHashMap<List<T>, Double>();
public HTTPRequest(){
}
public HTTPRequest(List<T> entry, Double value){
tableOfInitProbList.put(entry, value);
}
public void putListHTTPCharTable(List<T> uri, Double value){
tableOfInitProbList.put(uri, value);
}
}
The problem is the line 问题是线
addMe.putListHTTPCharTable(parseHTTPReqToCharbyChar(readHTTPURIFromTxt), ctr);
its in class filemanip openfile method inside the if statement. 它在if语句内的类filemanip openfile方法中。 I wrote putListHttpCharTable to take 2 parameters List < T > and Double but whenever I pass the argument parseHTTPReqToCharbyChar(readHTTPURIFromTxt) which has a return type of List there is a compile time error. 我写了putListHttpCharTable来接受2个参数List <T>和Double,但是每当我传递参数parseHTTPReqToCharbyChar(readHTTPURIFromTxt)时,它的返回类型为List,就会发生编译时错误。 It reads 它读
The method putListHTTPCharTable(List>, Double) in the type HTTPRequest> is not applicable for the arguments (List, Double) 类型为HTTPRequest>的方法putListHTTPCharTable(List>,Double)不适用于参数(List,Double)
I passed a method which has List < String > return type but somehow the compiler is trying to read it as List< List < T > > or a List < List < String > > instead of just being List < String >. 我通过了一个具有List <String>返回类型的方法,但是编译器不知何故试图将其读取为List <List <T>>或List <List <String>>而不是List <String>。 Is there anyway to fix this? 有没有什么办法解决这一问题?
Because : 因为:
HTTPRequest<List<String>>
means : 表示:
T <=> List<String>
so the compiler sees this (for your "addMe" variable): 因此编译器会看到以下内容(对于您的“ addMe”变量):
//Class HTTPRequest
public class HTTPRequest<List<String>> {
private LinkedHashMap<List<List<String>>, Double> tableOfInitProbList = new LinkedHashMap<List<List<String>>, Double>();
public HTTPRequest(){
}
public HTTPRequest(List<List<String>> entry, Double value){
tableOfInitProbList.put(entry, value);
}
public void putListHTTPCharTable(List<List<String>> uri, Double value){
tableOfInitProbList.put(uri, value);
}
}
You are setting T
to List<String>
您正在将T
设置为List<String>
Then the method is looking for a List<T>
so you end up with List<List<String>>
然后该方法正在寻找List<T>
因此您最终得到List<List<String>>
This should work if you change your class to: 如果您将班级更改为:
public class HTTPRequest<T> {
private LinkedHashMap<T, Double> tableOfInitProbList = new LinkedHashMap<List<T>, Double>();
public HTTPRequest(){
}
public HTTPRequest(T entry, Double value){
tableOfInitProbList.put(entry, value);
}
public void putListHTTPCharTable(T uri, Double value){
tableOfInitProbList.put(uri, value);
}
}
Alternately, if you only want to specify type of list with your generic parameter this declaration should work: 或者,如果您只想使用通用参数指定列表类型,则此声明应该起作用:
private HTTPRequest<String> addMe = new HTTPRequest<String>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.