IndexListIterator che implementa Iterator mediante un array list. Implementazione di Iterator mediante cursore : ElementIterator per PositionList. Aggiunti alcuni metodi e spostato le classi IndexListIterator,ElementIterator nel package Iterator.
This commit is contained in:
74
iterator/ElementIterator.java
Normal file
74
iterator/ElementIterator.java
Normal file
@@ -0,0 +1,74 @@
|
||||
package iterator;
|
||||
|
||||
import exceptions.BoundaryViolationException;
|
||||
import position.Position;
|
||||
import position.PositionList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:06
|
||||
*/
|
||||
|
||||
//iterator implementation with cursor for PositionList data structures.
|
||||
|
||||
public class ElementIterator<E> implements Iterator<E> {
|
||||
|
||||
public ElementIterator (PositionList<E> structure){
|
||||
|
||||
new_structure = structure;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (pos == null){
|
||||
if (new_structure. size() <= 0){
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
} else {
|
||||
|
||||
try {
|
||||
new_structure.next(pos);
|
||||
return true;
|
||||
}
|
||||
catch (BoundaryViolationException err){
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
if (pos == null){
|
||||
pos = new_structure.first();
|
||||
}else {
|
||||
pos = new_structure.next(pos);
|
||||
}
|
||||
return pos.element();
|
||||
} else{
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
PositionList<E> new_structure = null;
|
||||
Position<E> pos = null;
|
||||
|
||||
|
||||
}
|
||||
55
iterator/IndexListIterator.java
Normal file
55
iterator/IndexListIterator.java
Normal file
@@ -0,0 +1,55 @@
|
||||
package iterator;
|
||||
|
||||
import arraylist.ArrayIndexList;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
/**
|
||||
* Created with MONSTER.
|
||||
* User: xgiovio
|
||||
* Date: 06/04/2014
|
||||
* Time: 21:13
|
||||
*/
|
||||
|
||||
// generic iterator list creation using an arraylist. the class calling this metod should pass an array of E
|
||||
|
||||
public class IndexListIterator<E> implements Iterator<E> {
|
||||
|
||||
public IndexListIterator (E[] in_elements){
|
||||
for (int i = 0;i< in_elements.length;i++){
|
||||
data.add(i,in_elements[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
|
||||
if (pos <= data.size() -2){
|
||||
return true;
|
||||
}else {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public E next() throws NoSuchElementException {
|
||||
if (hasNext()){
|
||||
pos++;
|
||||
return data.get(pos);
|
||||
}else {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException ();
|
||||
}
|
||||
|
||||
private ArrayIndexList<E> data = new ArrayIndexList<E>();
|
||||
private int pos = -1;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user