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

48 lines
1.0 KiB
Java
Raw Normal View History

2022-12-22 22:27:39 +04:00
import java.util.ArrayList;
2022-12-20 21:50:09 +04:00
public class SetBattleshipGeneric <T>
{
2022-12-22 22:27:39 +04:00
private final ArrayList<T> _places;
private final int _maxCount;
public int Count() {
return _places.size();
2022-12-20 21:50:09 +04:00
}
2022-12-22 22:27:39 +04:00
public SetBattleshipGeneric(int count) {
2022-12-20 21:50:09 +04:00
2022-12-22 22:27:39 +04:00
_maxCount = count;
_places = new ArrayList<>();;
2022-12-20 21:50:09 +04:00
}
2022-12-22 22:27:39 +04:00
2022-12-20 21:50:09 +04:00
public int Insert(T battleship)
{
return Insert(battleship, 0);
}
public int Insert(T battleship, int position)
{
2022-12-22 22:27:39 +04:00
if (position >= _maxCount|| position < 0) return -1;
_places.add(position, battleship);
return position;
2022-12-20 21:50:09 +04:00
}
2022-12-22 22:27:39 +04:00
2022-12-20 21:50:09 +04:00
public T Remove (int position) {
2022-12-22 22:27:39 +04:00
if (position >= _places.size() || position < 0) return null;
T result = _places.get(position);
_places.remove(position);
2022-12-20 21:50:09 +04:00
return result;
}
public T Get(int position)
{
if (position >= Count() || position < 0)
{
return null;
}
2022-12-22 22:27:39 +04:00
return _places.get(position);
}
public Iterable<T> GetBattleships()
{
return _places;
2022-12-20 21:50:09 +04:00
}
}