如何删除对象中的属性,或数组中的元素
1个回答
展开全部
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;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询