2022-12-15 22:31:51 +04:00
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
public class SetShipGeneric<T> implements Iterable<T> {
|
|
|
|
private ArrayList<T> _places;
|
|
|
|
private final int _maxCount;
|
2022-12-15 22:28:43 +04:00
|
|
|
public int getCount() {
|
2022-12-15 22:31:51 +04:00
|
|
|
return _places.size();
|
2022-12-15 22:28:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public SetShipGeneric(int count)
|
|
|
|
{
|
2022-12-15 22:31:51 +04:00
|
|
|
_maxCount = count;
|
|
|
|
_places = new ArrayList<>();
|
2022-12-15 22:28:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public int Insert(T ship)
|
|
|
|
{
|
|
|
|
return Insert(ship, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int Insert(T ship, int position)
|
|
|
|
{
|
2022-12-15 22:31:51 +04:00
|
|
|
if (position < 0 || position > getCount() || _maxCount == getCount()) return -1;
|
|
|
|
_places.add(position,ship);
|
2022-12-15 22:28:43 +04:00
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Remove(int position)
|
|
|
|
{
|
2022-12-15 22:31:51 +04:00
|
|
|
if (position < 0 || position >= getCount()) return null;
|
|
|
|
T DelElement = (T) _places.get(position);
|
|
|
|
_places.remove(position);
|
2022-12-15 22:28:43 +04:00
|
|
|
return DelElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
public T Get(int position)
|
|
|
|
{
|
2022-12-15 22:31:51 +04:00
|
|
|
if (position < 0 || position >= getCount()) return null;
|
|
|
|
return _places.get(position);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Set(int position,T value)
|
|
|
|
{
|
|
|
|
if (position < _maxCount || position >= 0)
|
|
|
|
{
|
|
|
|
Insert(value, position);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Iterator<T> iterator() {
|
|
|
|
return _places.iterator();
|
2022-12-15 22:28:43 +04:00
|
|
|
}
|
|
|
|
}
|