[英]Java Two ArrayLists from One ArrayList
I have a List where I am storing 2 ArrayLists inside this. 我有一个列表,我在其中存储2个ArrayLists。
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: 这是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
Change your declaration of list in parseXml()
method. 在
parseXml()
方法中更改列表的声明。
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. 好了,您有两个
List<String>
并将所有条目(单个条目是String)添加到元数据列表。 If you use the list's get()
method, you will retreive the String at the specific index. 如果使用列表的
get()
方法,则将在特定索引处检索String。
Either store the two lists in an array to reaccess both or use a List<List<String>> metadatalist
. 将两个列表存储在数组中以重新访问两者,或者使用
List<List<String>> metadatalist
。 So you use the add()
method to add both lists to your metadatalist and can access each list again. 因此,您可以使用
add()
方法将两个列表都添加到元数据列表中,并可以再次访问每个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.