2022-10-21 16:04:51 +04:00
|
|
|
public class SetLocomotivesGeneric <T>
|
|
|
|
{
|
|
|
|
private final T[] _places;
|
|
|
|
|
|
|
|
public int Count() {
|
|
|
|
return _places.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
public SetLocomotivesGeneric(int count) {
|
|
|
|
_places = (T[]) new Object[count];
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean Insert (T locomotive) {
|
|
|
|
return Insert(locomotive, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean Insert (T locomotive, int position) {
|
|
|
|
if (position >= _places.length || position < 0) return false;
|
|
|
|
if (_places[position] == null) {
|
|
|
|
_places[position] = locomotive;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int emptyEl = -1;
|
|
|
|
for (int i = position + 1; i < Count(); i++)
|
|
|
|
{
|
2022-10-21 21:27:07 +04:00
|
|
|
if (_places[i] == null)
|
|
|
|
{
|
|
|
|
emptyEl = i;
|
|
|
|
break;
|
|
|
|
}
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
if (emptyEl == -1)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int i = emptyEl; i > position; i--)
|
|
|
|
{
|
|
|
|
_places[i] = _places[i - 1];
|
|
|
|
}
|
|
|
|
_places[position] = locomotive;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean Remove (int position) {
|
|
|
|
if (position >= _places.length || position < 0) return false;
|
|
|
|
_places[position] = null;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Get(int position)
|
|
|
|
{
|
|
|
|
if (position >= _places.length || position < 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return _places[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|