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

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.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.NoSuchElementException;
public class SetGeneric<T extends Object> { public class SetGeneric<T extends Object> {
/// <summary> /// <summary>
@ -81,11 +82,40 @@ public class SetGeneric<T extends Object> {
return null; return null;
return _places.get(position); return _places.get(position);
} }
/// <summary> /// <summary>
/// Проход по списку /// Проход по списку
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public Iterable<T> GetTrains(final Integer maxTrains) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() { 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> /// <param name="g"></param>
private void DrawObjects(Graphics2D g) 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 (train != null)
if (t != null)
{ {
t.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight); train.SetPosition((i % (_pictureWidth / _placeSizeWidth)) * _placeSizeWidth, (i / (_pictureWidth / _placeSizeWidth)) * _placeSizeHeight);
if (t instanceof DrawingLoco) if (train instanceof DrawingLoco)
((DrawingLoco) t).DrawTransport(g); ((DrawingLoco)train).DrawTransport(g);
else else
t.DrawTransport(g); train.DrawTransport(g);
} }
i++;
} }
} }
} }