查看我的Java通用';s通配符
在下面的代码中,我不知道我是否正确编码了最后一个方法,查看我的Java通用';s通配符,java,Java,在下面的代码中,我不知道我是否正确编码了最后一个方法,public boolean addAll(SimpleSetbasic of“?extensed SomeClass“,“?”表示作为SomeClass实例或SomeClass的任何子类的对象 addAll的唯一问题是它接受“SimpleSet,您的代码有几个问题: 您的循环永远不会超过第一次迭代,因为您在第一次迭代中返回true 您的迭代器方法中有一个无限递归。它只调用自己 你的循环永远不会超过第一次迭代,因为你在第一次迭代中返回true
public boolean addAll(SimpleSetbasic of“?extensed SomeClass“,“?”表示作为SomeClass实例或SomeClass的任何子类的对象
addAll的唯一问题是它接受“SimpleSet,您的代码有几个问题:
您的循环永远不会超过第一次迭代,因为您在第一次迭代中返回true
您的迭代器方法中有一个无限递归。它只调用自己
你的循环永远不会超过第一次迭代,因为你在第一次迭代中返回true!看起来你的添加和删除方法的逻辑可能有缺陷你的意思是我必须在addAll方法下返回false然后返回true??Ben Knoble:你的意思是如果(data.get(I).equals(x)){in add方法不起作用???为什么add和remove方法可能有缺陷???您建议如何修复它?我应该先返回false,然后返回true吗?为什么add和remove方法可能有缺陷???有人告诉我:您的循环永远不会超过第一次迭代,因为您在第一次迭代中返回true!…如何修复that?是返回false然后返回true!为什么添加和删除方法可能有缺陷???@java很简单:不要在循环中包含return
s。您可以将循环的结果累积到一个临时变量中,然后返回该变量的内容。
package set;
import java.util.ArrayList;
import java.util.Iterator;
public class ArraySet<E> implements SimpleSet<E> {
private ArrayList<E> data=new ArrayList<E>();
/**
* Constructs a new empty set.
*/
public ArraySet() {
}
/**
* Adds the specified element to this set, if it is not already present.
* post: x is added to the set if it is not already present
* @param x the element to be added
* @return true if the specified element was added
*/
public boolean add(E x) {
for(int i=0; i<data.size(); i++){
if(data.get(i).equals(x)){
System.out.println("x is already added");
} else {
data.add(x);
System.out.println("x is added to the set");
return true;
}
}
return false;
}
/**
* Removes the specified element from this set if it is present.
* post: x is removed if it was present
* @param x the element to remove - if present
* @return true if the set contained the specified element
*/
public boolean remove(Object x) {
for(int i=0; i<data.size(); i++){
if(data.get(i).equals(x)){
data.remove(x);
return false;
}
}
return true;
}
/**
* Returns true if this set contains the specified element.
* @param x the element whose presence is to be tested
* @return true if this set contains the specified element
*/
public boolean contains(Object x) {
for(int i=0; i<data.size(); i++){
if(data.get(i).equals(x)){
return true;
}
}
return false;
}
/**
* Returns true if this set contains no elements.
* @return true if this set contains no elements
*/
public boolean isEmpty() {
if(data.size()==0){
return true;
}
return false;
}
/**
* Returns the number of elements in this set.
* @return the number of elements in this set
*/
public int size() {
return data.size();
}
/**
* Returns an iterator over the elements in this set.
* @return an iterator over the elements in this set
*/
public Iterator<E> iterator() {
return this.iterator();
}
/**
* Adds all of the elements in the specified set, for which it is
* possible, to this set.
* post: all elements, for which it is possible, in the
* specified set are added to this set.
* @return true if this set changed as a result of the call
*/
public boolean addAll(SimpleSet<? extends E> s) {
for(E e : s){
data.add(e);
System.out.println("all elements, for which it is possible, in the specified set are added to this set. ");
return true;
}
return false;
}
}