gpt4 book ai didi

java - 从 Java 中的 Hashmap 数据循环遍历数组

转载 作者:行者123 更新时间:2023-11-29 09:06:52 33 4
gpt4 key购买 nike

我有从 Web 服务下载的 XML 文件,我遇到的问题是我无法让它在循环外正确显示数据。

XML 格式设置如下:

<section>
<outage>
<dummydata>test</dummydata>
</outage>
<outage>
<dummydata>test2</dummydata>
</outage>
</section>
<section>
<outage>
<dummydata>test3</dummydata>
<outage>
</section>

请注意,某些路段可能不止一次停电。正如您在下面看到的,计数是正确的,但一次中断来自 4。

01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.176: I/System.out(1924): Outage Count per sect 4
01-24 22:54:49.186: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.186: I/System.out(1924): Outage Count per sect 1
01-24 22:54:49.196: I/System.out(1924): Outages : 763674

这是完成所有工作的代码

for (int j = 0; j < arrPolygons.size(); j++) {

if (isPointInPolygon(point, arrPolygons.get(j).getPoints())) {
for (int outageInfo = 0; outageInfo < nodeList.getLength(); outageInfo++) {
outageData = new LinkedList<HashMap<String, String>>();
Node ouageInfoNode = nodeList.item(outageInfo);

Element ouageInfoElement = (Element) ouageInfoNode;
NodeList ouageInfoList = ouageInfoElement.getElementsByTagName("Outage");
System.out.println("Outage Count per sect "+ouageInfoList.getLength());
for (int outages = 0; outages < ouageInfoList.getLength(); outages++) {
Node outageNode = ouageInfoList.item(outages);
Element outageDataElement = (Element) outageNode;
NodeList outageDataList =outageDataElement.getElementsByTagName("outagenum");

outageDataElement = (Element) outageDataList.item(0);
outageDataList = outageDataElement.getChildNodes();

outagenum = ((Node) outageDataList.item(0))
.getNodeValue();

//This is what I want to loop
HashMap<String, String> map = new HashMap<String, String>();
map.put("outagenum", outagenum);
outageData.add(outages, map);
}

}
Toast.makeText(MainActivity.this, "Inside " + arrPolygons.get(j).getId()+" "+ outageData.get(j), Toast.LENGTH_LONG).show();

System.out.println("Outages : " + outageData.get(0));

}
}

}
}

我的结果应该是这样的

Index 0 - {test,test2}
Index 1 - {test3}

谁能告诉我在这个循环中我做错了什么?

最佳答案

 for (int outageInfo = 0; outageInfo < nodeList.getLength(); outageInfo++) {
outageData = new LinkedList<HashMap<String, String>>();

应该

         outageData = new LinkedList<HashMap<String, String>>();
for (int outageInfo = 0; outageInfo < nodeList.getLength(); outageInfo++) {

关于java - 从 Java 中的 Hashmap 数据循环遍历数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14515878/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com