Pibd-22_Presnyakova.V.V_Cat.../SetBoatsGeneric.java

54 lines
1.2 KiB
Java
Raw Normal View History

2022-12-23 11:15:23 +04:00
import java.util.ArrayList;
import java.util.Iterator;
2022-12-23 10:22:06 +04:00
public class SetBoatsGeneric<T> {
2022-12-23 11:15:23 +04:00
private ArrayList<T> _places;
private final int _maxCount;
2022-12-23 10:22:06 +04:00
public int Count() {
2022-12-23 11:15:23 +04:00
return _places.size();
2022-12-23 10:22:06 +04:00
}
public SetBoatsGeneric(int count) {
2022-12-23 11:15:23 +04:00
_maxCount=count;
_places = new ArrayList<>();
2022-12-23 10:22:06 +04:00
}
public int Insert(T boat) {
2022-12-23 11:15:23 +04:00
return Insert(boat, 0);
2022-12-23 10:22:06 +04:00
}
public int Insert(T boat, int position) {
2022-12-23 11:15:23 +04:00
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
_places.add(position,boat);
2022-12-23 10:22:06 +04:00
return position;
}
public T Remove(int position) {
2022-12-23 11:15:23 +04:00
if (position >= Count() || position < 0)
{
return null;
}
T ship = (T) _places.get(position);
_places.remove(ship);
return ship;
2022-12-23 10:22:06 +04:00
}
public T Get(int position) {
2022-12-23 11:15:23 +04:00
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();
2022-12-23 10:22:06 +04:00
}
}