For循环没有按照我想要的方式工作
我有一个非常小的问题,我似乎无法弄清楚。我正在尝试根据 ArrayList> 中的值类型提取数据并将其放入另一个ArrayList中。问题是 for 循环仅运行一次,在本例中我需要它遍历整个数组,然后将数据放入 unSuppressedData 数组列表中。
下面是 for 循环:
for (int x = 0; x < suppressedStatus.length; x++) {
for (int i = 0; i < availData.size(); i++) {
Hashtable<String,String> checkAvail = availData.get(i);
String itemStatus = checkAvail.get("loanStatus");
if (unSuppressedData.contains(checkAvail) == false) {
if (!(itemStatus.equals(suppressedStatus[x]))) {
Log.d("Item Status", itemStatus);
Log.d("Suppressed Status", suppressedStatus[x]);
unSuppressedData.add(checkAvail);
//break;
}
}
}
}
suppressedStatus 是一个字符串数组
availableData 是我想从中提取数据的数组列表
unSuppressedData 是我想要将数据放入的数组列表
我相信它只运行一次是由于这行代码:
if (unSuppressedData.contains(checkAvail) == false) {
但我需要这一行来检查我的 unSuppressdData 是否有数据,如果没有则将添加来自availData 的数据arraylist 转换为 unSuppressedData arraylist。
难道是我这段代码写错了?感谢对此的任何见解。
I have a extremely minor issue that I can't seem to figure out. I'm trying to extract data based on a type of value from an ArrayList> and place it into another ArrayList. The issue is that the for-loop only runs once, which in this case i need it to traverse the entire array and then place the data into the unSuppressedData arraylist.
Below is the for-loop:
for (int x = 0; x < suppressedStatus.length; x++) {
for (int i = 0; i < availData.size(); i++) {
Hashtable<String,String> checkAvail = availData.get(i);
String itemStatus = checkAvail.get("loanStatus");
if (unSuppressedData.contains(checkAvail) == false) {
if (!(itemStatus.equals(suppressedStatus[x]))) {
Log.d("Item Status", itemStatus);
Log.d("Suppressed Status", suppressedStatus[x]);
unSuppressedData.add(checkAvail);
//break;
}
}
}
}
suppressedStatus is a String array
availData is the arraylist i want to extract data from
unSuppressedData is the arraylist i want to place the data in
I believe that it only runs once is due to this line of code:
if (unSuppressedData.contains(checkAvail) == false) {
But i need to this line to check whether my unSuppressdData has the data, if no then will add the data from availData arraylist into unSuppressedData arraylist.
Could it be that i'm writing this piece of code wrongly? Appreciate any insights shed on this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于此类事情来说,一个很好的集合类型是 LinkedHashSet。因为它是一个集合,所以每个元素只能添加一次。作为一个散列,包含测试很快。 “链接”后,结果集将按插入顺序进行迭代。
A good collection type for this sort of thing is the LinkedHashSet. Because it's a set, each element can only be added once. Being a hash, the contains test is quick. Being 'linked' the resulting set is iterated in insertion order.