Java 如何创建只保留列表中包含在指定列表中的元素而不使用任何第三方的函数
我有一个类StudentArrayList继承接口SimpleArrayList SimpleArrayList具有以下功能:retainAllJava 如何创建只保留列表中包含在指定列表中的元素而不使用任何第三方的函数,java,arraylist,interface,Java,Arraylist,Interface,我有一个类StudentArrayList继承接口SimpleArrayList SimpleArrayList具有以下功能:retainAll /** * Retains only the elements in this list that are contained in the * specified student.SimpleArrayList. In other words, removes from this list all of * its elements that
/**
* Retains only the elements in this list that are contained in the
* specified student.SimpleArrayList. In other words, removes from this list all of
* its elements that are not contained in the specified student.SimpleArrayList.
*
* @param c
* student.SimpleArrayList containing elements to be retained in this
* list
* @return <tt>true</tt> if this list changed as a result of the call
*/
boolean retainAll(SimpleArrayList<E> c);
/**
*仅保留此列表中包含在中的元素
*指定的student.SimpleRayList。换句话说,从该列表中删除所有
*它的元素不包含在指定的student.SimpleArrayList中。
*
*@param c
*student.SimpleArrayList包含要保留在此列表中的元素
*名单
*@如果此列表因调用而更改,则返回true
*/
布尔保留(SimpleArrayList c);
我在StudentArrayList中创建了一个对应的函数,如下所示:
@Override
public boolean retainAll(SimpleArrayList c) {
if (c.size()==0){
this.clear();
return true;
}
int temp=this.size;
int count=0;
boolean check=false;
for(int i=0;i<this.size;i++){
for(int j=0;j<c.size();j++){
if(this.studentList[i].equals(c.get(j))) check=true;
}
if(!check) {
this.remove(i);
//this.size--;
check=false;
}
}
if(temp==this.size) return false;
return true;
@覆盖
公共布尔保留(SimpleArrayList c){
如果(c.size()==0){
这个.clear();
返回true;
}
int temp=此尺寸;
整数计数=0;
布尔检查=假;
对于(int i=0;i请尝试以下方法:
@Override
public boolean retainAll(SimpleArrayList c) {
if (c.size()==0){
this.clear();
return true;
}
int temp=this.size;
int count=0;
for(int i=0;i<this.size;i++){
boolean check=false;
for(int j=0;j<c.size();j++){
if(this.studentList[i].equals(c.get(j))) check=true;
}
if(!check) {
this.remove(i);
//this.size--;
check=false;
}
}
if(temp==this.size) return false;
return true;
@覆盖
公共布尔保留(SimpleArrayList c){
如果(c.size()==0){
这个.clear();
返回true;
}
int temp=此尺寸;
整数计数=0;
对于(int i=0;iSorry,但我们不能对语句“my coding get error answer”执行任何操作。使用。错误行为:如果两个列表都为空,即使列表没有更改,您的方法仍返回true
。好奇:计数的目的是什么?性能:设置检查=true
,您应该中断内部循环。无需保留sea正在重新整理c
列表。