Правлен SetGeneric
This commit is contained in:
parent
9f8be965e5
commit
650b42c43a
@ -55,7 +55,7 @@ public class PlanesGenericCollection<T extends DrawingPlane, U extends IMoveable
|
||||
private void DrawObjects(Graphics g) {
|
||||
int placesColumnCount = _pictureHeight / _placeSizeHeight;
|
||||
int placesRowCount = _pictureWidth / _placeSizeWidth;
|
||||
for (int i = 0; i < _collection.Count; i++) {
|
||||
for (int i = 0; i < _collection.Count(); i++) {
|
||||
// получение объекта
|
||||
var obj = _collection.Get(i);
|
||||
// установка позиции
|
||||
|
@ -1,12 +1,20 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class SetGeneric<T extends DrawingPlane> {
|
||||
private T[] _places;
|
||||
public int Count;
|
||||
private final ArrayList<T> _places;
|
||||
|
||||
public int Count() {
|
||||
return _places.size();
|
||||
}
|
||||
|
||||
private final int _maxCount;
|
||||
|
||||
public SetGeneric(int count) {
|
||||
_places = (T[]) new DrawingPlane[count];
|
||||
Count = count;
|
||||
_maxCount = count;
|
||||
_places = new ArrayList<>(count);
|
||||
}
|
||||
|
||||
public int Insert(T plane) {
|
||||
@ -15,51 +23,40 @@ public class SetGeneric<T extends DrawingPlane> {
|
||||
|
||||
public int Insert(T plane, int position) {
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count) {
|
||||
if (position < 0 || position >= _maxCount) {
|
||||
return -1;
|
||||
}
|
||||
// Проверка, что элемент массива по этой позиции пустой
|
||||
if (_places[position] != null) {
|
||||
// Проверка, что после вставляемого элемента в массиве есть пустой элемент
|
||||
int nullIndex = -1;
|
||||
for (int i = position + 1; i < Count; i++) {
|
||||
if (_places[i] == null) {
|
||||
nullIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Если пустого элемента нет, то выходим
|
||||
if (nullIndex < 0) {
|
||||
return -1;
|
||||
}
|
||||
// Сдвиг всех объектов, находящихся справа от позиции до первого пустого элемента
|
||||
int j = nullIndex - 1;
|
||||
while (j >= position) {
|
||||
_places[j + 1] = _places[j];
|
||||
j--;
|
||||
}
|
||||
}
|
||||
// Вставка по позиции
|
||||
_places[position] = plane;
|
||||
_places.add(position, plane);
|
||||
return position;
|
||||
}
|
||||
|
||||
public T Remove(int position) {
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count) {
|
||||
if (position < 0 || position >= Count()) {
|
||||
return null;
|
||||
}
|
||||
// Удаление объекта из массива, присвоив элементу массива значение null
|
||||
T plane = _places[position];
|
||||
_places[position] = null;
|
||||
T plane = _places.get(position);
|
||||
_places.set(position, null);
|
||||
return plane;
|
||||
}
|
||||
|
||||
public T Get(int position) {
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count) {
|
||||
if (position < 0 || position >= Count()) {
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
return _places.get(position);
|
||||
}
|
||||
|
||||
public void Set(int position, T plane) {
|
||||
// Проверка позиции
|
||||
// Проверка свободных мест в списке
|
||||
if (position < 0 || position >= _maxCount || Count() == _maxCount) {
|
||||
return;
|
||||
}
|
||||
// Вставка в список по позиции
|
||||
_places.set(position, plane);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user