// 答案是太能了:不用集合一样办到:(ArryList集合底层就是这么弄的)
// 方法很多种,软copy和硬copy,还有字符串桥接,随便举两种吧
public class ArrayTest {
public static void main(String[] args) {
int a1[]={1,3,4,5,0,0,9,6,0,5,4,7,6,7,0,5};
int a2[] = { 1, 0, 5,0,6,0,4, 1, 0 };
//测试第一种办法:
System.out.println("原数组:");
for (int n : a1)
System.out.print(n + ",");
// 删除元素!
a1=volume(a1,0);
System.out.println("\n现数组:");
for (int n : a1)
System.out.print(n + ",");
System.out.println("\n\n-----------------分割线--------------------\n");
//测试第二种办法:
System.out.println("原数组:");
for (int n : a2)
System.out.print(n + ",");
// 删除元素!
a2 = value(a2, 0);
System.out.println("\n现数组:");
for (int n : a2)
System.out.print(n + ",");
}// 方法1,浅复制!
private static int[] volume(int[] arr, int key) {
int count = 0;
for (int i = 0, j = arr.length - 1; i <= j; i++, j--) {
if (arr[i] == key && i != j)
count++;
if (arr[j] == key && i != j)
count++;
if (arr[i] == key && i == j)
count++;
}
int nrr[] = new int[arr.length - count];
count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key)
continue;
else
nrr[count++] = arr[i];
}
return arr = nrr;
}//方法2:这方法最简单,的搞法,用个字符串桥接一下即可!
private static int[] value(int[] arr, int key) {
StringBuilder str=new StringBuilder();
for (int i = 0; i < arr.length; i++)
if(arr[i]!=key)
str.append(arr[i]);
char[] chs=new String(str).toCharArray();
int[] orr=new int[chs.length];
for (int i = 0; i < orr.length; i++) {
orr[i]=chs[i]-'0';
}
return arr=orr;
}//方法3:深复制,算了!
}