我试图删除一些元素ArrayList迭代它像这样:
for (String str : myArrayList) {
if (someCondition) {
myArrayList.remove(str);
}
}
Run Code Online (Sandbox Code Playgroud)
当然,我ConcurrentModificationException试图在迭代时同时从列表中删除项目时得到一个myArrayList.有没有一些简单的解决方案来解决这个问题?
我在Android中同步代码的概念中遗漏了一些东西.
脚本
屏幕上总是绘制3个项目.每个图像都存储在ArrayList(lstGraphics)中.为此,我使用SurfaceView.一旦用户点击图像,图像就会被移除市场并且将添加新的市场.
代码示例:
AnimationHideThread
...
@Override
public void run() {
Canvas c;
while (run) {
c = null;
try {
c = panel.getHolder().lockCanvas(null);
synchronized (panel.getHolder()) {
panel.updatePhysics();
panel.manageAnimations();
panel.onDraw(c);
}
} finally {
if (c != null) {
panel.getHolder().unlockCanvasAndPost(c);
}
}
}
}
...
Run Code Online (Sandbox Code Playgroud)
所以你可以先看看updatePhysics().这意味着我计算每个图像移动到的方向.在这里,我还将删除列表中的点击图像.之后,我检查是否需要在manageAnimations()的列表中添加一个新项目,然后最后一步绘制整个事物.
public class Panel extends SurfaceView implements SurfaceHolder.Callback {
....
public void manageAnimations()
{
synchronized (this.getHolder()) {
...
while (lstGraphics.size()<3) {
lstGraphics.add(createRandomGraphic());
}
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (getHolder()) { …Run Code Online (Sandbox Code Playgroud) 我正在为观点实施"拖放".当拖动开始时,我将视图的可见性设置为INVISIBLE,然后,如果拖动被中断 - 返回到VISIBLE:
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// Skipped some code
boolean dragStarted = v.startDrag(data, shadowBuilder, v, 0);
if (dragStarted) {
v.setVisibility(View.INVISIBLE)
}
}
}
Run Code Online (Sandbox Code Playgroud)
和:
if (event.getAction() == DragEvent.ACTION_DRAG_ENDED) {
View droppedView = (View) event.getLocalState();
droppedView.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
当调用"Drag ended"事件时,我会遇到异常:
E/AndroidRuntime(7118): FATAL EXCEPTION: main
E/AndroidRuntime(7118): java.util.ConcurrentModificationException
E/AndroidRuntime(7118): at java.util.HashMap$HashIterator.nextEntry(HashMap.java:792)
E/AndroidRuntime(7118): at java.util.HashMap$KeyIterator.next(HashMap.java:819)
E/AndroidRuntime(7118): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1046)
E/AndroidRuntime(7118): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1048)
E/AndroidRuntime(7118): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1048)
E/AndroidRuntime(7118): at android.view.ViewGroup.dispatchDragEvent(ViewGroup.java:1048)
E/AndroidRuntime(7118): at android.view.ViewRootImpl.handleDragEvent(ViewRootImpl.java:3471)
E/AndroidRuntime(7118): …Run Code Online (Sandbox Code Playgroud) 当我执行以下代码时,我得到ConcurrentModificationException
Collection<String> myCollection = Collections.synchronizedList(new ArrayList<String>(10));
myCollection.add("123");
myCollection.add("456");
myCollection.add("789");
for (Iterator it = myCollection.iterator(); it.hasNext();) {
String myObject = (String)it.next();
System.out.println(myObject);
myCollection.remove(myObject);
//it.remove();
}
Run Code Online (Sandbox Code Playgroud)
为什么我得到异常,即使我使用Collections.synchronizedList?
当我将myCollection更改为
ConcurrentLinkedQueue<String> myCollection = new ConcurrentLinkedQueue<String>();
Run Code Online (Sandbox Code Playgroud)
我没有得到那个例外.
java.util.concurrent中的ConcurrentLinkedQueue与Collections.synchronizedList有何不同?