54 lines
1.2 KiB
Java
54 lines
1.2 KiB
Java
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
public class SetBoatsGeneric<T> {
|
|
private ArrayList<T> _places;
|
|
private final int _maxCount;
|
|
public int Count() {
|
|
return _places.size();
|
|
}
|
|
|
|
public SetBoatsGeneric(int count) {
|
|
_maxCount=count;
|
|
_places = new ArrayList<>();
|
|
}
|
|
|
|
public int Insert(T boat) {
|
|
return Insert(boat, 0);
|
|
|
|
}
|
|
|
|
public int Insert(T boat, int position) {
|
|
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
|
|
_places.add(position,boat);
|
|
return position;
|
|
}
|
|
|
|
public T Remove(int position) {
|
|
if (position >= Count() || position < 0)
|
|
{
|
|
return null;
|
|
}
|
|
T ship = (T) _places.get(position);
|
|
_places.remove(ship);
|
|
return ship;
|
|
}
|
|
|
|
public T Get(int position) {
|
|
if (position >= Count() || position<0)
|
|
{
|
|
return null;
|
|
}
|
|
return _places.get(position);
|
|
}
|
|
public void Set(int position,T value)
|
|
{
|
|
if (position < _maxCount || position >= 0)
|
|
{
|
|
Insert(value,position);
|
|
}
|
|
}
|
|
public Iterator<T> iterator() {
|
|
return _places.iterator();
|
|
}
|
|
}
|