我有一个我想处理的字节数组。我的目标是去除固定尺寸的图案。如何删除数组中的元素并缩小其大小?
我使用下面的代码:
byte buffer[] = {1,2,3,4,5,6,7,8,9,10};
int position; // position of the patern in the stream
int length = buffer.length();
int pattern_size = 64; // size of the patern to delete
for(i = position; i<length; i++){
// Shift the bytes
buffer[i] = buffer[i+pattern_size];
}
for(i = length; i>=(length-pattern_size); i--){
// Adding 0s at the end to make up for the loss of data
buffer[i]=0;
}
data_removed = data_removed + pattern_size;
正如你可以看到我在字节数组的末尾添加零。我实际上更喜欢减小数组的大小。我怎样才能做到这一点 ?
你的标题应该是'在array',因为'List'是有点一个动态数组。为了做到这一点,你需要创建一个大小为“originalArray-1”的数组。之后,您可以使用'System.arrayCopy'将一个数组中的所有值复制到另一个数组中。 – SomeJavaGuy
谢谢,我编辑了我的标题。确实创建一个新阵列是一个好主意。 –