PIbd-22_Bondarenko_M.S._War.../SetShipGeneric.java
Макс Бондаренко 097cb7f944 готово 4
2022-12-15 22:31:51 +04:00

55 lines
1.2 KiB
Java

import java.util.*;
public class SetShipGeneric<T> implements Iterable<T> {
private ArrayList<T> _places;
private final int _maxCount;
public int getCount() {
return _places.size();
}
public SetShipGeneric(int count)
{
_maxCount = count;
_places = new ArrayList<>();
}
public int Insert(T ship)
{
return Insert(ship, 0);
}
public int Insert(T ship, int position)
{
if (position < 0 || position > getCount() || _maxCount == getCount()) return -1;
_places.add(position,ship);
return position;
}
public T Remove(int position)
{
if (position < 0 || position >= getCount()) return null;
T DelElement = (T) _places.get(position);
_places.remove(position);
return DelElement;
}
public T Get(int position)
{
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();
}
}