如题所述
public class Delect {
public static void main(String[] args) {
A a1 = new A(1);
A a2 = new A(2);
A a3 = new A(3);
A a4 = new A(4);
A a5 = new A(5);
A[] a = {a1,a2,a3,a4,a5};
A[] b = delect(a,a2);//删除指定元素a2
for(int i = 0;i<b.length;++i)
System.out.print(b[i].x+" ");
}
public static A[] delect(A[] a,A x){
A[] b = new A[a.length-1];
int j = 0;
for(int i = 0;i < a.length;++i){
if(a[i] == x)
++i;
b[j++] = a[i];
}
return b;//返回一个删除后的新数组
}
}
class A{
int x;
public A(int x){
this.x = x;
}
}
public static void main(String[] args) {
A a1 = new A(1);
A a2 = new A(2);
A a3 = new A(3);
A a4 = new A(4);
A a5 = new A(5);
A[] a = {a1,a2,a3,a4,a5};
A[] b = delect(a,a2);//删除指定元素a2
for(int i = 0;i<b.length;++i)
System.out.print(b[i].x+" ");
}
public static A[] delect(A[] a,A x){
A[] b = new A[a.length-1];
int j = 0;
for(int i = 0;i < a.length;++i){
if(a[i] == x)
++i;
b[j++] = a[i];
}
return b;//返回一个删除后的新数组
}
}
class A{
int x;
public A(int x){
this.x = x;
}
}
温馨提示:答案为网友推荐,仅供参考