PIbd-21_Markov_D.P._Contain.../SetShipGeneric.java

57 lines
1.3 KiB
Java
Raw Normal View History

2022-12-08 21:15:20 +04:00
import java.util.ArrayList;
import java.util.Iterator;
public class SetShipGeneric<T extends Object> implements Iterable<T>{
private ArrayList<T> _places;
private final int _maxCount;
//private final Object[] _places;
2022-12-08 21:14:01 +04:00
public int Count()
{
2022-12-08 21:15:20 +04:00
return _places.size();
2022-12-08 21:14:01 +04:00
}
public SetShipGeneric(int count)
{
2022-12-08 21:15:20 +04:00
_maxCount=count;
_places = new ArrayList<>();
2022-12-08 21:14:01 +04:00
}
public int Insert(T ship)
{
return Insert(ship, 0);
}
public int Insert(T ship, int position)
{
2022-12-08 21:15:20 +04:00
if (position < 0 || position > Count() || _maxCount == Count()) return -1;
_places.add(position,ship);
2022-12-08 21:14:01 +04:00
return position;
}
public T Remove(int position)
{
2022-12-08 21:15:20 +04:00
if (position >= Count() || position < 0)
2022-12-08 21:14:01 +04:00
{
return null;
}
2022-12-08 21:15:20 +04:00
T ship = (T) _places.get(position);
_places.remove(ship);
2022-12-08 21:14:01 +04:00
return ship;
}
public T Get(int position)
{
2022-12-08 21:15:20 +04:00
if (position >= Count() || position<0)
2022-12-08 21:14:01 +04:00
{
return null;
}
2022-12-08 21:15:20 +04:00
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-08 21:14:01 +04:00
}
}