Pibd-22_Presnyakova.V.V_Cat.../SetBoatsGeneric.java
2022-12-23 10:22:06 +04:00

45 lines
1.0 KiB
Java

public class SetBoatsGeneric<T> {
private T[] _places;
public int Count() {
return _places.length;
}
public SetBoatsGeneric(int count) {
_places = (T[]) (new Object[count]);
}
public int Insert(T boat) {
for (int i = 0; i < _places.length; i++) {
if (_places[i] == null) {
_places[i] = boat;
return i;
}
}
return -1;
}
public int Insert(T boat, int position) {
int index = position;
while (_places[index] != null && index < _places.length) index++;
if (index == _places.length) return -1;
for (int i = index; i > position; --i) _places[i] = _places[i - 1];
_places[position] = boat;
return position;
}
public T Remove(int position) {
if (position >= _places.length) return null;
T res = _places[position];
_places[position] = null;
return res;
}
public T Get(int position) {
return _places[position];
}
}