forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package iterator.examples.vectors; | ||
|
||
public class Client { | ||
public static void main(String argv[]) { | ||
Vector vector = new Vector(5); | ||
|
||
//Creación del iterador | ||
VectorIterator iterador = vector.iterador(); | ||
|
||
//Recorrido con el iterador | ||
while (iterador.hasNext()) | ||
System.out.println(iterador.next()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package iterator.examples.vectors; | ||
|
||
public class Vector { | ||
public int[] _datos; | ||
|
||
public Vector(int valores){ | ||
_datos = new int[valores]; | ||
for (int i = 0; i < _datos.length; i++){ | ||
_datos[i] = i; | ||
} | ||
} | ||
|
||
public int getValor(int pos){ | ||
return _datos[pos]; | ||
} | ||
|
||
public void setValor(int pos, int valor){ | ||
_datos[pos] = valor; | ||
} | ||
|
||
public int dimension(){ | ||
return _datos.length; | ||
} | ||
|
||
public VectorIterator iterador(){ | ||
return new VectorIterator(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package iterator.examples.vectors; | ||
|
||
public class VectorIterator { | ||
private int[] _vector; | ||
private int _posicion; | ||
|
||
public VectorIterator(Vector vector) { | ||
_vector = vector._datos; | ||
_posicion = 0; | ||
} | ||
|
||
public boolean hasNext(){ | ||
if (_posicion < _vector.length) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
public Object next(){ | ||
int valor = _vector[_posicion]; | ||
_posicion++; | ||
return valor; | ||
} | ||
} |