0

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

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

它在 if 语句中的类 filemanip openfile 方法中。我写了 putListHttpCharTable 来获取 2 个参数 List < T > 和 Double 但是每当我传递返回类型为 List 的参数 parseHTTPReqToCharbyChar(readHTTPURIFromTxt) 时,都会出现编译时错误。它读到

HTTPRequest> 类型中的 putListHTTPCharTable(List>, Double) 方法不适用于参数 (List, Double)

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

4

2 回答 2

3

因为 :

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

    }
于 2012-10-03T13:57:52.763 回答
2

您正在设置TList<String>

然后该方法正在寻找一个List<T>所以你最终得到List<List<String>>

如果您将课程更改为:

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

}

或者,如果您只想使用泛型参数指定列表类型,则此声明应该有效:

private HTTPRequest<String> addMe = new HTTPRequest<String>();

于 2012-10-03T14:00:31.170 回答