Java 为什么在删除JSON对象时此循环会过早退出?
我有一个简单的JSON数组Java 为什么在删除JSON对象时此循环会过早退出?,java,arrays,json,Java,Arrays,Json,我有一个简单的JSON数组 { "food" : [ { "name" : "apple" }, { "name" : "orange" }, { "name" : "peach" }, { "name" : "carrot" }, { "name" : "lettuce" } ] } 但是,当我尝试删除除保留一个之外的所有对象时,删除for循环会
{
"food" : [
{
"name" : "apple"
},
{
"name" : "orange"
},
{
"name" : "peach"
},
{
"name" : "carrot"
},
{
"name" : "lettuce"
}
]
}
但是,当我尝试删除除保留一个之外的所有对象时,删除for循环会先发制人地退出
String itemToKeepsName = "carrot";
JSONArray list = wrappedFood.getJSONArray("food");
JSONObject addThisItemBack = null; // be ready to make a new space in memory.
println("number of items in list: " + list.length()); // prints 5.
int found = -1;
for(int i = 0; i < list.length(); ++i) {
if(addThisItemBack.equals(list.getJSONObject(i).getString("name"))) {
found = i;
addThisItemBack = new JSONObject(list.getJSONObject(i).toString());
}
}
if (found >= 0) { // found at index 3.
println("number of items before removeall loop: " + list.length()); // prints 5.
for (int i = 0; i < list.length(); ++i) {
println("removed item: " + i); // prints 0, 1, 2.
list.remove(i);
}
println("adding item: " + addThisItemBack); // {"food":["name":"carrot"}]}
list.put(addThisItemBack);
}
而不是:
{
"food" : [
{
"name" : "carrot"
}
]
}
如何确保在添加项目之前列表已完全清空?我是否忽略了一些显而易见的事情?这是JSON操作的秘密吗 每次删除元素时,
列表会缩小。这个
for (int i = 0; i < list.length(); ++i) {
println("removed item: " + i); // prints 0, 1, 2.
list.remove(i);
}
或带有
Iterator iter=list.Iterator();
while(iter.hasNext()){
JsonValue.ValueType value=iter.next();
println(“删除:+值);
iter.remove();
}
请注意链接的Javadoc中的注释:如果在迭代过程中以任何方式(而不是通过调用此方法)修改了基础集合,则迭代器的行为是未指定的。每次删除元素时,列表会缩小。这个
for (int i = 0; i < list.length(); ++i) {
println("removed item: " + i); // prints 0, 1, 2.
list.remove(i);
}
或带有
Iterator iter=list.Iterator();
while(iter.hasNext()){
JsonValue.ValueType value=iter.next();
println(“删除:+值);
iter.remove();
}
请注意链接Javadoc中的注意事项:如果在迭代过程中以任何方式(而不是调用此方法)修改了基础集合,则迭代器的行为是不确定的。感谢接受的答案,我意识到我的问题就像在开始循环之前捕获项数一样简单
if (found >= 0) { // found at index 3.
int countOfItemsToRemove = list.length(); // do this
println("number of items before removeall loop: " + countOfItemsToRemove); // prints 5.
for (int i = 0; i < countOfItemsToRemove; ++i) {
println("removed item: " + i); // prints 0, 1, 2.
list.remove(i);
}
println("adding item: " + addThisItemBack); // {"food":["name":"carrot"}]}
list.put(addThisItemBack);
}
如果(发现>=0){//在索引3处发现。
int countOfItemsToRemove=list.length();//执行此操作
println(“removeall循环之前的项目数:“+countOfItemsToRemove”);//打印5。
对于(int i=0;i
多亏了公认的答案,我意识到我的问题就像在开始循环之前捕获项目数量一样简单
if (found >= 0) { // found at index 3.
int countOfItemsToRemove = list.length(); // do this
println("number of items before removeall loop: " + countOfItemsToRemove); // prints 5.
for (int i = 0; i < countOfItemsToRemove; ++i) {
println("removed item: " + i); // prints 0, 1, 2.
list.remove(i);
}
println("adding item: " + addThisItemBack); // {"food":["name":"carrot"}]}
list.put(addThisItemBack);
}
如果(发现>=0){//在索引3处发现。
int countOfItemsToRemove=list.length();//执行此操作
println(“removeall循环之前的项目数:“+countOfItemsToRemove”);//打印5。
对于(int i=0;i
啊,这很有道理,我不知道为什么我之前没有注意到这一点。啊,这很有道理,我不知道为什么我之前没有注意到。
if (found >= 0) { // found at index 3.
int countOfItemsToRemove = list.length(); // do this
println("number of items before removeall loop: " + countOfItemsToRemove); // prints 5.
for (int i = 0; i < countOfItemsToRemove; ++i) {
println("removed item: " + i); // prints 0, 1, 2.
list.remove(i);
}
println("adding item: " + addThisItemBack); // {"food":["name":"carrot"}]}
list.put(addThisItemBack);
}