有这样一个问题。我有一个方法可以将文件中的字节读入一个数组,以及在这个数组中搜索最长字节序列的方法。
private int element;
private int lastElement;
private int length;
private byte[] readByteFromFile(File name) throws IOException {
return Files.readAllBytes(name.toPath());
}
private void searchByte(byte[] byteMass) {
for (int i = 0; i < byteMass.length; i++) {
int count = 0;
for (int j = i + 1; j < byteMass.length; j++) {
if (byteMass[i + count] == byteMass[j]) {
if (count >= length) {
length = count + 1;
element = i;
lastElement = j - count;
}
count++;
} else {
count = 0;
}
}
}
}
假设我的文件包含这样一个数字序列:
444478126354444
在处理的情况下,我的方法将推断出第一次出现在 0,第二次出现在 11 和序列长度 = 4
但如果我有这样一个序列
133333444478126354444
然后我的方法会推断出第一次出现在1,第二次出现在2,序列的长度为4
如何修复,该方法继续正常工作?
隔江千里
胡子哥哥
相关分类