PIbd-21_KozyrevSS_GasolineT.../SetGeneric.java

57 lines
1.3 KiB
Java

public class SetGeneric <T extends Object>{
private final T[] _places;
public int Count() {return _places.length;}
public SetGeneric(int count)
{
_places = (T[]) new Object[count];
}
public int Insert(T tanker)
{
return Insert(tanker, 0);
}
public int Insert(T tanker, int position)
{
if (position < 0 || position >= Count())
return -1;
if (_places[position] == null)
{
_places[position] = tanker;
return position;
}
int index = -1;
for (int i = position; i < Count(); i++)
{
if (_places[i] == null)
{
index = i; break;
}
}
if (index < 0)
return -1;
for (int i = index; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = tanker;
return position;
}
public boolean Remove(int position)
{
if (position < 0 || position > Count())
return false;
_places[position] = null;
return true;
}
public T Get(int position)
{
if (position < 0 || position >= Count())
{
return null;
}
return (T)_places[position];
}
}