Реализован класс SetGeneric
This commit is contained in:
parent
9f805eeba2
commit
0e17b7a751
72
ProjectStormtrooper/SetGeneric.java
Normal file
72
ProjectStormtrooper/SetGeneric.java
Normal file
@ -0,0 +1,72 @@
|
||||
package ProjectStormtrooper;
|
||||
|
||||
public class SetGeneric<T extends DrawingPlane> {
|
||||
private T[] _places;
|
||||
public int Count() {return _places.length;}
|
||||
public SetGeneric(int count)
|
||||
{
|
||||
_places = (T[]) new DrawingPlane[count];
|
||||
}
|
||||
public int Insert(T plane)
|
||||
{
|
||||
return Insert(plane, 0);
|
||||
}
|
||||
public int Insert(T plane, int position)
|
||||
{
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count())
|
||||
{
|
||||
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;
|
||||
return position;
|
||||
}
|
||||
public T Remove(int position)
|
||||
{
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// Удаление объекта из массива, присвоив элементу массива значение null
|
||||
T plane = _places[position];
|
||||
_places[position] = null;
|
||||
return plane;
|
||||
}
|
||||
public T Get(int position)
|
||||
{
|
||||
// Проверка позиции
|
||||
if (position < 0 || position >= Count())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return _places[position];
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user