Pibd-23_Zhelovanov_D.Y._Bat.../SetBattleshipGeneric.java

48 lines
1.0 KiB
Java

import java.util.ArrayList;
public class SetBattleshipGeneric <T>
{
private final ArrayList<T> _places;
private final int _maxCount;
public int Count() {
return _places.size();
}
public SetBattleshipGeneric(int count) {
_maxCount = count;
_places = new ArrayList<>();;
}
public int Insert(T battleship)
{
return Insert(battleship, 0);
}
public int Insert(T battleship, int position)
{
if (position >= _maxCount|| position < 0) return -1;
_places.add(position, battleship);
return position;
}
public T Remove (int position) {
if (position >= _places.size() || position < 0) return null;
T result = _places.get(position);
_places.remove(position);
return result;
}
public T Get(int position)
{
if (position >= Count() || position < 0)
{
return null;
}
return _places.get(position);
}
public Iterable<T> GetBattleships()
{
return _places;
}
}