-2
我有一個包含10個對象的數組。 然後,我用2個對象創建了上述數組的子數組。 我想要java代碼創建另一個子陣列與其餘8個對象。如何從給定數組創建對象的子數組?
對於如:
int[] a=[0,1,2,3,4,5,6,7,8,9]
int[] b=[4,7]
我想
int [] c=[0,1,2,3,5,6,8,9,]
我有一個包含10個對象的數組。 然後,我用2個對象創建了上述數組的子數組。 我想要java代碼創建另一個子陣列與其餘8個對象。如何從給定數組創建對象的子數組?
對於如:
int[] a=[0,1,2,3,4,5,6,7,8,9]
int[] b=[4,7]
我想
int [] c=[0,1,2,3,5,6,8,9,]
你可以只用一個簡單的代碼是這樣的:
int[] c = new int[a.length - b.length]; //that is, assuming there is no repeated elements
int count = 0;
main: for (int i = 0; i < a.length; i++) {
for (int j = 0; j < b.length; j++)
if (a[i] == b[j])
continue main; //the element should be eliminated
c[count] = a[i];
count++;
}
如果你不知道的大小results數組,請使用ArrayList或創建一個與a大小相同的大小,然後忽略空白空間。
請不要重新發布問題。然後,在問這樣一個問題之前,你應該始終表明你已經嘗試了什麼。 –
您可以循環訪問數組'a',而不在數組'b'中的值將它們添加到數組'c'中? – user959631
重複刪除服務器,釋放空間? – user959631