因此,第一个方法iterator()调用AIterator类的构造函数,每次调用构造函数时都会将索引重置为0。所以每当我尝试使用iterator()。hasNext()和iteratro()迭代一个集合时,next()就会变成一个无限循环。不知道如何处理这个问题。你能帮忙吗?我也在底部粘贴了AIterator课程。它们属于同一个包装。
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.lang.UnsupportedOperationException;
/**
* Comment me!!!
* @author
* @version
*/
// equals method needs work probably
// Works till add()
// Enters into infinite loop when trying to run addAll()
// Everything in between /* ... */ and // and /** .... **/ are experiments or thought :)
public class Aset<R> implements Set151Interface<R>{
// TODO: write code
public static final int DEFAULT_CAPACITY = 10;
private R[] genArray;
@SuppressWarnings("unchecked")
public Aset(int size) {
if(size < 0) {
throw new IllegalArgumentException();
}
genArray = (R[]) new Object[size];
}
public Aset() {
this(DEFAULT_CAPACITY);
}
@SuppressWarnings("unchecked")
public Aset(Collection<? extends R> collection) {
this(DEFAULT_CAPACITY);
if (collection == null) {
throw new IllegalArgumentException();
}
genArray = (R[]) collection.toArray();
//addAll(collection);
/*
Iterator<R> myIterator = this.iterator();
while(myIterator.hasNext()) {
R current = myIterator.next();
if(current == null) {
throw new IllegalArgumentException();
}
if(!(this.contains(current))) {
this.add(current);
System.out.println("test");
}
}
*/
}
/**
* Provides a method for traversing an array using a well-known Java interface.
* @return an interator that can traverse the given ADT.
**/
@Override
public Iterator<R> iterator() {
Iterator<R> aSetIterator = new AIterator<R>(genArray, size());
//AIterator<R> aSetIterator = this.iterator();
return aSetIterator;
}
@Override
public boolean add(R q) {
if(q == null) {
throw new IllegalArgumentException();
}
/*
while(iterator().hasNext()) {
if(this.equals(q)){
return false;
}
}
*/
checkCapacity();
genArray[size()] = q;
return true;
}
@Override
public void clear() {
while(iterator().hasNext()) {
remove(iterator().next());
}
}
@Override
public boolean isEmpty() {
return genArray.length == 0;
}
@Override
public boolean addAll(Collection<? extends R> c) {
int check = size();
if(checkType(c)) {
@SuppressWarnings("unchecked")
Aset<R> newCollection = (Aset<R>) c;
System.out.println(newCollection.size());
while(newCollection.iterator().hasNext()) {
add(newCollection.iterator().next());
newCollection.iterator().next();
}
}
/*
R currentCollection = c.iterator().next();
while(iterator().hasNext()) {
while(c.iterator().hasNext()) {
if(!this.contains(currentCollection)) {
add(currentCollection); // or change iterator().next() with "this"
}
}
}
*/
if(check == size()) {
return true;
}
return false;
}
@Override
public int size() {
int aSetSize = 0;
for (int i=0; i < genArray.length; i++) {
if(genArray[i] != null) {
aSetSize++;
}
}
return aSetSize;
/*int aSetSize = 0;
while(iterator().hasNext()) {
if(iterator().next() != null) {
aSetSize++;
}
}
return aSetSize;
*/
}
public int getCapacity() {
if(genArray.length <= DEFAULT_CAPACITY){
return DEFAULT_CAPACITY;
}
return genArray.length;
}
private void checkCapacity() {
if(size() >= genArray.length) {
@SuppressWarnings("unchecked")
R[] newGenArray = (R[]) new Object[genArray.length + DEFAULT_CAPACITY];
System.arraycopy(genArray, 0, newGenArray, 0, size());
genArray = newGenArray;
}
}
@Override
public Object[] toArray() {
throw new UnsupportedOperationException();
}
@Override
public <T> T[] toArray(T[] array){
throw new UnsupportedOperationException();
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + Arrays.hashCode(genArray);
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof Aset)) {
return false;
}
@SuppressWarnings("unchecked")
Aset<R> other = (Aset<R>) obj;
if (!(size() == other.size()) && compareArrays(genArray, other.genArray)) {
System.out.println("2 :( ");
System.out.println(genArray.length);
System.out.println(other.genArray.length);
return false;
}
return true;
}
// probably needs to be sorted before using this method
private boolean compareArrays(R a[], R b[]) {
Arrays.sort(a);
Arrays.sort(b);
int shortArrLength = Math.min(a.length, b.length);
for (int i = 0; i<shortArrLength; i++) {
if(!(a[i].hashCode() == b[i].hashCode())) {
return false;
}
}
return true;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
@Override
public String toString() {
return "<" + Arrays.toString(genArray) + ">";
}
*/
@Override
public String toString() {
if (size() == 0) {
return "<>";
}
String aSet = "";
for(R element:genArray) {
if(element != null) {
aSet += ", " + element.toString();
}
}
return "<" + aSet.substring(2) + ">";
}
private boolean checkType(Object other) {
if(other.getClass().equals(this.getClass())) {
return true;
}
return false;
}
}
AIterator class 我已经对next()进行了更改,因为我认为索引在类中的任何地方都没有增加,并且它可能会导致无限循环如果我还没有在return语句之前添加index ++。
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This class creates an iterator to travers an Aset object.
* @author William Kreahling
* @version 10/1/2014
*/
public class AIterator<E> implements Iterator<E>{
private int index;
private E[] data;
private int size;
/**
* Created an iterator for an Aset Object.
* @param data the generic array for Aset.
* @param size the number of elements (cardinality) of the set.
*/
public AIterator(E[] data, int size) {
index = 0;
this.size = size;
this.data = data;
}
/**
* Returns the next element of the collection.
* @return the next element if the set.
* @throws NoSuchElementException if there are no elements left in the set.
*/
public E next() {
if (index >= size) {
throw new NoSuchElementException("Cannot access element " + index +
"This set has a total of " + size + " elements.");
}
index++; // this line has been added
return data[index]; // this line has been edited from -> return data[index++];
}
/**
* Determines if there are elements left in the set.
* @return true if there are more elements in the set.
*/
public boolean hasNext() {
return index >= 0 && index < size;
}
public void remove() {
throw new UnsupportedOperationException("AIterator does not support remove");
}
}
答案 0 :(得分:1)
使用如下。
Iterator<XX> it = theASet.iterator();
//in loop
it.next();
不要使用theASet.iterator()
和theASet.next()
,因为它会创建一个新的迭代器实例,其索引始终为零。