PIbd-23_Mochalov_D.V._Locom.../SetLocomotivesGeneric.java

62 lines
1.4 KiB
Java

public class SetLocomotivesGeneric <T>
{
private final T[] _places;
public int Count() {
return _places.length;
}
public SetLocomotivesGeneric(int count) {
_places = (T[]) new Object[count];
}
public int Insert (T locomotive) {
return Insert(locomotive, 0);
}
public int Insert (T locomotive, int position) {
if (position >= _places.length || position < 0) return -1;
if (_places[position] == null) {
_places[position] = locomotive;
return position;
}
int emptyEl = -1;
for (int i = position + 1; i < Count(); i++)
{
if (_places[i] == null)
{
emptyEl = i;
break;
}
}
if (emptyEl == -1)
{
return -1;
}
for (int i = emptyEl; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = locomotive;
return position;
}
public T Remove (int position) {
if (position >= _places.length || position < 0) return null;
T result = _places[position];
_places[position] = null;
return result;
}
public T Get(int position)
{
if (position >= _places.length || position < 0)
{
return null;
}
return _places[position];
}
}