简体   繁体   中英

Java Two ArrayLists from One ArrayList

I have a List where I am storing 2 ArrayLists inside this.

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

In the other method, I want to access these two lists. I am trying this code:

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

Its not allowing me to do above line showing error like: Cannot convert from String to List. Please tell me how can I access these two lists and do further process:

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

Here is parseXml method:

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

Change your declaration of list in parseXml() method.

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

to

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

And add element to it as

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

Then you are able to retrieve those lists as single elements later.

Well, you have two List<String> and add all entries (a single entry is a String) to the metadatalist. If you use the list's get() method, you will retreive the String at the specific index.

Either store the two lists in an array to reaccess both or use a List<List<String>> metadatalist . So you use the add() method to add both lists to your metadatalist and can access each list again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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