這是我的數組:int[] test= new int[] {1,0,1,0,0,0,0,0,0,0}
如何從int數組中刪除零?
現在我需要從這個數組中刪除所有的零所以它會顯示如下:輸出:{1,1}
我想這個代碼從這個link-StackOverFlow但沒有爲我工作。
int j = 0;
for (int i = 0; i < test.length; i++) {
if (test[i] != 0)
test[j++] = test[i];
}
int[] newArray = new int[j];
System.arraycopy(test, 0, newArray, 0, j);
return newArray;
請幫我解決這個問題。
有什麼問題? –
它不會從它刪除0只是返回相同。 – Sri
我剛剛試過你的代碼,它工作正常! newArray = {1,1} – grig