gpt4 book ai didi

java - 需要 java 泛型参数列表 的帮助

转载 作者:行者123 更新时间:2023-12-01 17:29:09 25 4
gpt4 key购买 nike

大家好,我对这段代码有疑问:

//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);
}

}

问题出在线路上

addMe.putListHTTPCharTable(parseHTTPReqToCharbyChar(readHTTPURIFromTxt), ctr);

其位于类 filemanip openfile 方法中的 if 语句中。我编写了 putListHttpCharTable 来接受 2 个参数 List < T > 和 Double,但是每当我传递返回类型为 List 的参数 parseHTTPReqToCharbyChar(readHTTPURIFromTxt) 时,就会出现编译时错误。上面写着

The method putListHTTPCharTable(List>, Double) in the type HTTPRequest> is not applicable for the arguments (List, Double)

我传递了一个具有 List 返回类型的方法,但不知何故,编译器试图将其读取为 List > 或 List > 而不仅仅是 List 。有办法解决这个问题吗?

最佳答案

因为:

HTTPRequest<List<String>>

意思是:

T <=> List<String> 

所以编译器会看到这个(对于你的“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);
}

}

关于java - 需要 java 泛型参数列表 <T> 的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12709992/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com