简体   繁体   English

Java中不兼容的返回类型

[英]Incompatible return type in Java

I have the following method: (from this WSDL URL: http://services.aonaware.com/DictService/DictService.asmx?WSDL ) 我有以下方法:(从此WSDL URL: http : //services.aonaware.com/DictService/DictService.asmx? WSDL)

private static WordDefinition define(java.lang.String word) {
    com.aonaware.services.webservices.DictService service = new com.aonaware.services.webservices.DictService();
    com.aonaware.services.webservices.DictServiceSoap port = service.getDictServiceSoap();

    return port.define(word);
}

It takes a word from a jTextField and returns a list of its definitions. 它从jTextField中获取一个单词并返回其定义列表。

I have declared the following list: 我宣布了以下清单:

List<WordDefinition> def = new ArrayList<WordDefinition>();

I want to print the result I get from the method. 我想打印从方法中得到的结果。 So I made the following loop: 所以我做了以下循环:

    for (int i=0; i< def.size(); i++){
        System.out.println(Arrays.deepToString(def));
    }

where: 哪里:

    String str = jTextField1.getText();
    def = define(str);

The error is get is the following: 错误是以下内容:

Incompatible types: List cannot be converted to Object[] 不兼容的类型:列表无法转换为Object []

Any help would be greatly appreciated. 任何帮助将不胜感激。

您需要将列表转换为Arrays.deepToString()才能接受的数组: Arrays.deepToString(def.toArray())

I see a lot of confusion. 我看到很多困惑。

First of all, the static method define returns you a WordDefinition , not a List<WordDefinition> , so the line 首先,静态方法define返回一个WordDefinition ,而不是List<WordDefinition> ,因此该行

def = define(str)

should give you a compile error. 应该给你一个编译错误。

Anyway, assuming List<WordDefinition> def is actually the variable you have with content, either you use Arrays.deepToString OR you iterate over the List, not both. 无论如何,假设List<WordDefinition> def实际上是您所拥有的内容变量,或者您使用Arrays.deepToString或对List进行迭代,而不是同时对两者进行迭代。

If you want to iterate, you can just do: 如果要迭代,可以执行以下操作:

for(WordDefinition wd: def){
    System.out.println(wd);
}

or 要么

Arrays.deepToString(def.toArray());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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