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];
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:59:22 +04:00
|
|
|
public int Insert (T locomotive) {
|
2022-10-21 16:04:51 +04:00
|
|
|
return Insert(locomotive, 0);
|
|
|
|
}
|
|
|
|
|
2022-10-21 21:59:22 +04:00
|
|
|
public int Insert (T locomotive, int position) {
|
|
|
|
if (position >= _places.length || position < 0) return -1;
|
2022-10-21 16:04:51 +04:00
|
|
|
if (_places[position] == null) {
|
|
|
|
_places[position] = locomotive;
|
2022-10-21 21:59:22 +04:00
|
|
|
return position;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2022-10-21 21:59:22 +04:00
|
|
|
return -1;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
for (int i = emptyEl; i > position; i--)
|
|
|
|
{
|
|
|
|
_places[i] = _places[i - 1];
|
|
|
|
}
|
|
|
|
_places[position] = locomotive;
|
2022-10-21 21:59:22 +04:00
|
|
|
return position;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
2022-10-21 21:59:22 +04:00
|
|
|
public T Remove (int position) {
|
|
|
|
if (position >= _places.length || position < 0) return null;
|
|
|
|
T result = _places[position];
|
2022-10-21 16:04:51 +04:00
|
|
|
_places[position] = null;
|
2022-10-21 21:59:22 +04:00
|
|
|
return result;
|
2022-10-21 16:04:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public T Get(int position)
|
|
|
|
{
|
|
|
|
if (position >= _places.length || position < 0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return _places[position];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|