Массив заменен на список

This commit is contained in:
prodigygirl 2022-11-06 22:25:36 +04:00
parent 5fc726afe3
commit 8aa6989be2
2 changed files with 29 additions and 44 deletions

View File

@ -47,13 +47,9 @@ public class MapWithSetArmoredCarsGeneric<T extends IDrawingObject, U extends Ab
public Image ShowOnMap()
{
Shaking();
for (int i = 0; i < _setCars.getCount(); i++)
for (var armoredCar : _setCars.GetArmoredCars())
{
var car = _setCars.Get(i);
if (car != null)
{
return _map.CreateMap(_pictureWidth, _pictureHeight, car);
}
return _map.CreateMap(_pictureWidth, _pictureHeight, armoredCar);
}
return new BufferedImage(_pictureWidth, _pictureHeight, BufferedImage.TYPE_INT_BGR);
}
@ -135,12 +131,16 @@ 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;
for (var armoredCar : _setCars.GetArmoredCars())
{
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);
}
armoredCar.SetObject(i % width * _placeSizeWidth + 5, i / width * _placeSizeHeight + 10, _pictureWidth, _pictureHeight);
armoredCar.DrawingObject((Graphics2D) g);
i++;
}
}
public SetArmoredCarsGeneric<T> get_setCars() {
return _setCars;
}
}

View File

@ -1,16 +1,17 @@
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.*;
public class SetArmoredCarsGeneric<T> {
private T[] _places;
private ArrayList<T> _places;
private int maxCount;
public SetArmoredCarsGeneric(int count)
{
_places = (T[]) new Object[count];
_places = new ArrayList<T>();
maxCount = count;
}
public int getCount() {
return _places != null ? _places.length : 0;
return _places != null ? _places.size() : 0;
}
public int Insert(T armoredCar)
@ -20,47 +21,31 @@ public class SetArmoredCarsGeneric<T> {
public int Insert(T armoredCar, int position)
{
if (position < 0 || position >= getCount())
if (getCount() >= maxCount || position < 0 || position >= maxCount)
return -1;
if (!(_places[position] == null))
{
int index_empty = -1;
// поиск первого пустого элемента
for (int i = position + 1; i < getCount(); i++)
{
if (_places[i] == null)
{
index_empty = i;
}
}
if (index_empty == -1)
return -1;
else
{
for (int i = index_empty; i > position; i--)
{
_places[i] = _places[i - 1];
}
}
}
_places[position] = armoredCar;
_places.add(position, armoredCar);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= getCount())
if (position < 0 || position >= _places.size())
return null;
T armoredCar = _places[position];
_places[position] = null;
T armoredCar = _places.get(position);
_places.remove(position);
return armoredCar;
}
public T Get(int position)
{
if (position < 0 || position >= getCount())
if (position < 0 || position >= maxCount)
return null;
return _places[position];
return _places.get(position);
}
public Iterable<T> GetArmoredCars()
{
return () -> _places.stream().filter(Objects::nonNull).iterator();
}
}