PIbd-21_Markov_D.P._Contain.../SetShipGeneric.java
2022-12-08 21:14:01 +04:00

66 lines
1.4 KiB
Java

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