PIbd-23_Minhasapov_R.H._Exc.../SetTracktorGeneric.java

65 lines
1.4 KiB
Java
Raw Permalink Normal View History

public class SetTracktorGeneric<T> {
private final Object[] _places;
public int getCount() {
return _places.length;
}
public SetTracktorGeneric(int count) {
_places = new Object[count];
}
public int insert(T tracktor) {
return insert(tracktor, 0);
}
public int insert(T tracktor, int position) {
if (position < 0 || position >= getCount()) {
return -1;
}
if (_places[position] == null) {
_places[position] = tracktor;
return position;
}
int tmp = -1;
for(int i = position; i < getCount(); i++)
{
if (_places[i] == null)
{
tmp = i;
break;
}
}
if (tmp != -1)
{
System.arraycopy(_places, position, _places, position + 1, tmp - position);
_places[position] = tracktor;
return position;
}
return -1;
}
@SuppressWarnings("unchecked")
public T remove(int position) {
if (position < getCount() && position >= 0)
{
T temp = (T) _places[position];
_places[position] = null;
return temp;
}
return null;
}
@SuppressWarnings("unchecked")
public T get(int position) {
if (position < 0 || position >= getCount()) {
return null;
}
return (T) _places[position];
}
}