我有一个列表,我在其中存储2个ArrayLists。

metadataList.addAll(countryList);
metadataList.addAll(languageList);


在另一种方法中,我想访问这两个列表。我正在尝试此代码:

List<String> countriesList = metadataValues.get(0);
List<String> languageList = metadataValues.get(1);


它不允许我在显示以下错误的行上进行操作:无法从字符串转换为列表。请告诉我如何访问这两个列表并进行进一步处理:

List<String> metadataValues = parseXml(metadata.xml);


System.out.println("metadataValues in first method ---->" + metadataValues.size());
List<String> countriesList = metadataValues.get(0);
List<String> languageList = metadataValues.get(1);
StringBuilder sb = new StringBuilder();
StringBuilder sb1 = new StringBuilder();

if (countriesList.size() == 1 || languageList.size() == 1) {
    for (String cnt: countriesList) {
        video.setCountry(cnt);
    }
    for (String language: languageList) {
        video.setLanguage(language);
    }
}
if (countriesList.size() >= 2 || languageList.size() >= 2) {
    for (String s: countriesList) {
        sb.append(s);
        sb.append(",");
    }
    for (String s1: languageList) {
        sb1.append(s1);
        sb1.append(",");
    }
}
if (sb.length() != 0) {
    sb.deleteCharAt(sb.length() - 1);
    System.out.println("StringBuilder Value ------>" + sb.toString());
    video.setCountry(sb.toString());
}
if (sb1.length() != 0) {
    sb1.deleteCharAt(sb1.length() - 1);
    System.out.println("StringBuilder Value ------>" + sb1.toString());
    video.setLanguage(sb1.toString());
}


这是parseXml方法:

private static List < String > parseXml(String xmlData)
throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    InputSource src = new InputSource();
    src.setCharacterStream(new StringReader(xmlData));
    Document doc = builder.parse(src);
    NodeList nodes = doc.getElementsByTagName("metadata");
    Element line = null;
    Element line1 = null;
    List<String> countryList = new ArrayList<String>();
    List<String> languageList = new ArrayList<String>();
    List<String> metadataList = new ArrayList<String>();
    for (int i = 0; i < nodes.getLength(); i++) {
        Element element = (Element) nodes.item(i);
        NodeList countryNodes = element.getElementsByTagName("Country");
        NodeList languageNodes = element.getElementsByTagName("Language");
        for (int j = 0; j < countryNodes.getLength(); j++) {
            for (int k = 0; k < languageNodes.getLength(); k++) {
                if (countryNodes != null) {
                    line = (Element) countryNodes.item(j);
                    if (line != null) {
                        countryList.add(getCharacterDataFromElement(line));
                    }
                }
                if (languageNodes != null) {
                    line1 = (Element) languageNodes.item(k);
                    if (line1 != null) {
                        languageList
                            .add(getCharacterDataFromElement(line1));
                    }
                }
            }
        }
        metadataList.addAll(countryList);
        metadataList.addAll(languageList);
        // System.out
        // .println("countryList.size() ----->" + countryList.size());
    }
    return metadataList;

}

最佳答案

list != list of list


parseXml()方法中更改列表声明。

List<String> metadataList = new ArrayList<String>();




List<List<String>> metadataList = new ArrayList<List<String>>();


并添加元素为

metadataList.add(countryList);
metadataList.add(languageList);


然后,您以后可以将这些列表作为单个元素检索。

07-23 19:30