0

我正在做一个程序来压缩文件(表示为 Byte 的 ArrayList ),在某些时候我必须用单个字节替换所有出现的预定义“字符串”(“字符串”作为字节序列,而不是 Java 语言中的字符串)。

Byte 的 ArrayList 类型的“预定义字符串”存放在变量 opt_word 中,其长度存放在变量 opt_length 中,且始终 >=2

我在标有“HERE”的地方收到并发修改异常。进一步的调试表明,异常发生在第一次替换后的循环迭代中。

我知道其他人问过类似的问题,例如这里这里,但我的情况与他们的情况完全不同。我使用标准 for 循环。

            CopyOnWriteArrayList<Integer> removal_indexes = new CopyOnWriteArrayList<Integer>();
            for(int j=0, l=0; j <= encoded.get(k).size() - opt_length; ++j, ++l)
            {
                List<Byte> str = encoded.get(k).subList(j, j + opt_length);
                if (str.equals(opt_word))  // <-- here
                {
                    removal_indexes.add(l);
                    j += opt_length - 1;
                }
            }

            for(int l=0; l < removal_indexes.size(); ++l)
            {
                encoded.get(k).set(removal_indexes.get(l), (byte)(lowr + lengths.size()));
                for(int i=1; i < opt_length; ++i)
                    encoded.get(k).remove(removal_indexes.get(l)+1);
            }
4

2 回答 2

3

这与您链接到的其他情况相同。remove() 方法更改 Arraylist 的大小。在迭代时更改 Arraylist 大小会导致您提到的并发修改错误。解决方案是跟踪要在其他列表中删除的项目,然后在 for 循环完成后将其删除。

于 2013-06-30T20:09:33.407 回答
1

@David Pitre 已经指出了问题所在。将 ArrayList 更改为 CopyOnWriteArrayList

更新:

我尝试做你想做的事,即搜索和替换,它对我有用。我实现了一个示例代码。看看它是否适合你。

public class Search {
List<Byte> fileToCompress;  // Your File
List<Byte> opt_word = new ArrayList<Byte>(); // "String" to search for in the "fileToCompress"
Byte replacement; // Replacement for "String"

public static void main(String args[]) {
    Search s = new Search();

    s.display();
    s.findAndReplace();
    System.out.println("_____________________");
    s.display();
}

public Search() {
    fileToCompress = new CopyOnWriteArrayList<Byte>();

    fileToCompress.add((byte)1);
    fileToCompress.add((byte)3);
    fileToCompress.add((byte)3);
    fileToCompress.add((byte)4);
    fileToCompress.add((byte)5);
    fileToCompress.add((byte)3);
    fileToCompress.add((byte)4);
    fileToCompress.add((byte)6);

    opt_word = new ArrayList<Byte>();

    opt_word.add((byte)3);
    opt_word.add((byte)4);

    replacement = new Byte((byte)0);
}

public void findAndReplace() {
    for(int i=0; i<fileToCompress.size(); i++) {
        boolean isFound = false;
        if(fileToCompress.get(i).equals(opt_word.get(0))) {
            isFound = checkMatch(i);
            if(isFound) {
                replace(i);
            }
        }
    }
}

private boolean checkMatch(int index) {
    boolean isFound = true;
    for(int i=0; i<opt_word.size(); i++) {
        if(!opt_word.get(i).equals(fileToCompress.get(index + i))) {
            isFound = false;
            break;
        }
    }
    return isFound;
}

private void replace(int index) {
    for(int i=0 ; i<opt_word.size(); i++) {
        fileToCompress.remove(index);
    }

    fileToCompress.add(index, replacement);
}

public void display() {
    for(Byte b : fileToCompress) {
        System.out.println(b);
    }
}
}
于 2013-06-30T20:40:51.247 回答