我有一個我想處理的字節數組。我的目標是去除固定尺寸的圖案。如何刪除數組中的元素並縮小其大小?
我使用下面的代碼:
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
謝謝,我編輯了我的標題。確實創建一個新陣列是一個好主意。 –