PIbd-22_Bondarenko_M.S._War.../SetShipGeneric.java

55 lines
1.3 KiB
Java
Raw Normal View History

2022-12-15 22:28:43 +04:00
public class SetShipGeneric<T> {
private T[] _places;
public int getCount() {
return _places.length;
}
public SetShipGeneric(int count)
{
_places = (T[])(new Object[count]);
}
private boolean CanInsert(int position)
{
for (int i = position; i < _places.length; ++i)
if (_places[i] == null) return true;
return false;
}
public int Insert(T ship)
{
return Insert(ship, 0);
}
public int Insert(T ship, int position)
{
if (position < 0 || position > _places.length) return -1;
if (_places[position] != null && CanInsert(position))
{
for (int i = _places.length - 1; i > position; --i)
{
if (_places[i] == null)
{
_places[i] = _places[i - 1];
_places[i - 1] = null;
}
}
}
_places[position] = ship;
return position;
}
public T Remove(int position)
{
T DelElement = _places[position];
_places[position] = null;
return DelElement;
}
public T Get(int position)
{
if (position < 0 || position > _places.length) return null;
return _places[position];
}
}