теперь рэйнжБэйздЦикл

This commit is contained in:
Timourka 2023-10-24 02:41:12 +04:00
parent accd1149e2
commit c70b820dbe
2 changed files with 118 additions and 87 deletions

View File

@ -2,6 +2,7 @@ package laba1Loco;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object> {
/// <summary>
@ -81,11 +82,40 @@ public class SetGeneric<T extends Object> {
return null;
return _places.get(position);
}
/// <summary>
/// Проход по списку
/// </summary>
/// <returns></returns>
public Iterable<T> GetTrains(final Integer maxTrains) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return _places.iterator();
return new Iterator<T>() {
private int currentIndex = 0;
private int count = 0;
@Override
public boolean hasNext() {
return currentIndex < _places.size() && (maxTrains == null || count < maxTrains);
}
@Override
public T next() {
if (hasNext()) {
count++;
return _places.get(currentIndex++);
}
throw new NoSuchElementException();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}

View File

@ -112,17 +112,18 @@ public class TrainsGenericCollection<T extends DrawingTrain, U extends IMoveable
/// <param name="g"></param>
private void DrawObjects(Graphics2D g)
{
for (int i = 0; i < _collection.Count(); i++)
int i = 0;
for (T train : _collection.GetTrains(100))
{
T t = _collection.Get(i);
if (t != null)
if (train != null)
{
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t instanceof DrawingLoco)
((DrawingLoco) t).DrawTransport(g);
train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (train instanceof DrawingLoco)
((DrawingLoco)train).DrawTransport(g);
else
t.DrawTransport(g);
train.DrawTransport(g);
}
i++;
}
}
}