idsIterator.remove() 抛出 IllegalStateException
idsIterator.remove() throws IllegalStateException
我正在尝试使用 listIterator 的 remove
方法。但是,我收到 IllegalStateException
这是我的代码:
private List<Category> findDescendantsOfCurrentCategory() {
List<Category> categoryList = new ArrayList<>(mCategoryList);
List<Category> descendantCategories = new ArrayList<>();
Stack<String> categoriesIds = new Stack<>();
categoriesIds.push(mCategoryId);
ListIterator<Category> categoryListIterator = categoryList.listIterator();
ListIterator<String> idsIterator = categoriesIds.listIterator();
while (idsIterator.hasNext()) {
String parentId = idsIterator.next();
while (categoryListIterator.hasNext()) {
Category category = categoryListIterator.next();
if (category.getParentId().equals(parentId)) {
idsIterator.add(category.getId());
descendantCategories.add(category);
}
categoryListIterator.remove();
}
idsIterator.remove();
}
return descendantCategories;
}
堆栈跟踪:
03-09 23:11:33.160 25298-25298/com.example.clients.someoneplatform.dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clients.someoneplatform.dev, PID: 25298
java.lang.IllegalStateException
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:67)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.findDescendantsOfCurrentCategory(ListCategoriesFragment.java:186)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.startAddEditCategoryForModification(ListCategoriesFragment.java:154)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.access0(ListCategoriesFragment.java:39)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.onCategoryModifyButtonClicked(ListCategoriesFragment.java:124)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesAdapter$ViewHolder.onModifyButtonClicked(ListCategoriesAdapter.java:134)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesAdapter$ViewHolder_ViewBinding.doClick(ListCategoriesAdapter$ViewHolder_ViewBinding.java:47)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
ListIterator 上的文档 (https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#remove()) 介绍了删除方法:"It can be made only if add(E) has not been called after the last call to next or previous."
所以你基本上必须在调用 remove
之前重写你不使用 add 函数的代码
我正在尝试使用 listIterator 的 remove
方法。但是,我收到 IllegalStateException
这是我的代码:
private List<Category> findDescendantsOfCurrentCategory() {
List<Category> categoryList = new ArrayList<>(mCategoryList);
List<Category> descendantCategories = new ArrayList<>();
Stack<String> categoriesIds = new Stack<>();
categoriesIds.push(mCategoryId);
ListIterator<Category> categoryListIterator = categoryList.listIterator();
ListIterator<String> idsIterator = categoriesIds.listIterator();
while (idsIterator.hasNext()) {
String parentId = idsIterator.next();
while (categoryListIterator.hasNext()) {
Category category = categoryListIterator.next();
if (category.getParentId().equals(parentId)) {
idsIterator.add(category.getId());
descendantCategories.add(category);
}
categoryListIterator.remove();
}
idsIterator.remove();
}
return descendantCategories;
}
堆栈跟踪:
03-09 23:11:33.160 25298-25298/com.example.clients.someoneplatform.dev E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.clients.someoneplatform.dev, PID: 25298
java.lang.IllegalStateException
at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:67)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.findDescendantsOfCurrentCategory(ListCategoriesFragment.java:186)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.startAddEditCategoryForModification(ListCategoriesFragment.java:154)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.access0(ListCategoriesFragment.java:39)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesFragment.onCategoryModifyButtonClicked(ListCategoriesFragment.java:124)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesAdapter$ViewHolder.onModifyButtonClicked(ListCategoriesAdapter.java:134)
at com.example.clients.someoneplatform.ui.categories.list.ListCategoriesAdapter$ViewHolder_ViewBinding.doClick(ListCategoriesAdapter$ViewHolder_ViewBinding.java:47)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21209)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5525)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
ListIterator 上的文档 (https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html#remove()) 介绍了删除方法:"It can be made only if add(E) has not been called after the last call to next or previous."
所以你基本上必须在调用 remove
之前重写你不使用 add 函数的代码