小编Dam*_*ian的帖子

为什么一个循环抛出一个ConcurrentModificationException,而另一个不抛出?

我在编写旅行推销员计划时遇到过这种情况.对于内循环,我尝试了一个

for(Point x:ArrayList<Point>) {
// modify the iterator
}
Run Code Online (Sandbox Code Playgroud)

但是当向该列表添加另一个点时会导致ConcurrentModicationException被抛出.

但是,当我将循环更改为

for(int x=0; x<ArrayList<Point>.size(); x++) {
// modify the array
}
Run Code Online (Sandbox Code Playgroud)

循环运行良好,没有抛出异常.

两个for循环,那么为什么一个抛出异常而另一个没有呢?

java iterator concurrentmodification

10
推荐指数
2
解决办法
5134
查看次数

使用插入排序进行降序?

我正在尝试学习如何使用插入排序,这是我使用的主要代码:

for (j = 1; j < num.length; j++)  // Start with 1 (not 0)
{
    key = num[ j ];
    for(i = j - 1; (i >= 0) && (num[ i ] < key); i--) // Smaller values are moving up
    {
        num[ i+1 ] = num[ i ];
    }
    num[ i+1 ] = key;  // Put the key in its proper location
 }
Run Code Online (Sandbox Code Playgroud)

但是,我尝试将 - 更改为 + 以尝试将输出更改为降序,但我更加困惑。

这是我正在使用的完整代码:

public class InsertionSort {

    public static void main(String[] args) …
Run Code Online (Sandbox Code Playgroud)

java sorting

2
推荐指数
1
解决办法
2万
查看次数

将Java数组分配给非数组属性 - 可Serilizable

为什么第一行下面的方法编译而第二行不编译?我希望两者都失败.

import java.io.Serializable;

public class ArrayConversions {
    Serializable serial = new Serializable[5];
    Runnable run = new Runnable[5];
}
Run Code Online (Sandbox Code Playgroud)

java arrays

2
推荐指数
1
解决办法
66
查看次数

标签 统计

java ×3

arrays ×1

concurrentmodification ×1

iterator ×1

sorting ×1