54 lines
1.3 KiB
Java
54 lines
1.3 KiB
Java
import java.util.ArrayList;
|
|
import java.util.Iterator;
|
|
|
|
public class SetPlanesGeneric<T extends Object> implements Iterable<T> {
|
|
private ArrayList<T> _places;
|
|
private final int _maxCount;
|
|
public int Count() {
|
|
return _places.size();
|
|
}
|
|
|
|
public SetPlanesGeneric(int count) {
|
|
_maxCount=count;
|
|
_places = new ArrayList<>();
|
|
}
|
|
|
|
public int Insert(T plane) {
|
|
return Insert(plane, 0);
|
|
}
|
|
|
|
public int Insert(T plane, int position) {
|
|
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
|
|
_places.add(position,plane);
|
|
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);
|
|
}
|
|
}
|
|
@Override
|
|
public Iterator<T> iterator() {
|
|
return _places.iterator();
|
|
}
|
|
} |