如何使用两种不同的通用类型复制列表



我在一个类中有follwoing代码

List<String> list = new ArrayList<String>(objType.getParameters());

要将列表复制到列表中,您可以简单地这样做:List<String> list = new ArrayList<String>(objType.getParameters());

,但这是objType.getParameters()列表是字符串。现在,在我的情况下不是这样,而是一个参数对象。

public class Object_type {
    public String object_type;
    public Property properties;
    private List<Parameter> parameters;
    public List<Restriction> restriction;
    public List<Parameter> getParameters() {
        return parameters;
    }
}

和我的参数类只是一个带有字符串和整数的结构。

public class Parameter {
    String parameter;
    String data_type;
    String description;
    int min_no;
    int max_no;
    int order1;
    int nested;
    String default1;
    String format;
}

我有一个必须像那样的List<String>,而必须这样的List<Parameter>那么,在这种情况下,如何将我的列表复制到我的类似中?collection.copy均无法正常工作。

// Create a list of node and compile the xpath expression
            NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(
                    xmlDocument, XPathConstants.NODESET);
            arr = new String[nodeList.getLength()][constants.getParamColumns().length];
            // Loop thru the list and add to string array.
            for (int i = 0; i < nodeList.getLength()
                    / constants.getParamColumns().length; i++) {
                for (int j = 0; j < constants.getParamColumns().length; j++) {
                    arr[i][j] = nodeList.item(
                            j + (i * constants.getParamColumns().length))
                            .getTextContent();
                    list.add(nodeList.item(
                            j + (i * constants.getParamColumns().length))
                            .getTextContent());
                    System.out.println("current size in list : " + list.size());
                }
            }
            setArr(arr);

                        Add list to List<Parameter> here <-----
            System.out.println(" size of list : "
                    + objType.getParameters().size());

你不能。相同,您不能将一个不兼容的类型变量分配给另一个不兼容的类型变量,例如"字符串s = new parameter()"。

做得很模糊,您可以通过操纵接收到的字符串然后将参数对象添加到参数列表中。

只有在您可以操纵接收的字符串并确切地确定想要的哪些信息到参数对象的情况下,它才能正常工作。

希望这会有所帮助!

祝你好运!

您的插管分配参数为字符串。需要更改列表的类型

List<Parameter > list=objType.getParameters(); //UPDATED

最新更新