add iterator

This commit is contained in:
Zara28 2022-11-05 13:29:51 +04:00
parent b7dba8c76a
commit c5b0aa9297
3 changed files with 26 additions and 17 deletions

Binary file not shown.

View File

@ -47,14 +47,15 @@ public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends Ab
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setCars.getCount(); i++)
while(_setCars.iter.hasNext())
{
var car = _setCars.Get(i);
var car = _setCars.iter.next();
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
}
@ -112,12 +113,13 @@ public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends Ab
private void DrawArmoredCars(Graphics g)
{
int width = _pictureWidth / _placeSizeWidth;
for (int i = 0; i < _setCars.getCount(); i++)
int i = 0;
while(_setCars.iter.hasNext())
{
if (_setCars.Get(i) != null) {
_setCars.Get(i).SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setCars.Get(i).DrawingObject((Graphics2D) g);
}
_setCars.iter.next().SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
_setCars.iter.next().DrawingObject((Graphics2D) g);
i++;
}
}
}

View File

@ -1,12 +1,15 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;
public class SetArmoredCarsGeneric<T> {
private ArrayList<T> _places;
private int _MaxCount;
Iterator<T> iter = _places.iterator();
public SetArmoredCarsGeneric(int count)
{
_places = new ArrayList<T>(count);
_MaxCount = count;
_places = new ArrayList<T>();
}
public int getCount() {
@ -15,7 +18,8 @@ public class SetArmoredCarsGeneric<T> {
public int Insert(T armoredCar)
{
return Insert(armoredCar, 0);
if(_places.size()+1 < _MaxCount) return Insert(armoredCar, 0);
else return -1;
}
public int Insert(T armoredCar, int position)
@ -50,17 +54,20 @@ public class SetArmoredCarsGeneric<T> {
public T Remove(int position)
{
if (position < 0 || position >= getCount())
if (position < 0 || position >= getCount() || _places.get(position) == null)
return null;
T armoredCar = _places.get(position);
_places.set(position, null);
return armoredCar;
}
public T Get(int position)
{
if (position < 0 || position >= getCount())
return null;
return _places.get(position);
}
public T getMachine()
{
if(iter.hasNext())
{
return iter.next();
}
else return null;
}
}