51 lines
1.3 KiB
Java
51 lines
1.3 KiB
Java
package ProjectTankHard.Generics;
|
|
|
|
public class SetGeneric<T extends Object>{
|
|
private final Object[] _places;
|
|
public int Count;
|
|
public SetGeneric(int count){
|
|
_places = new Object[count];
|
|
Count = count;
|
|
}
|
|
public int Insert(T monorail){
|
|
return Insert(monorail, 0);
|
|
}
|
|
public int Insert(T monorail, int position){
|
|
if(!(position >= 0 && position < Count)) return -1;
|
|
|
|
if (_places[position] == null) {
|
|
_places[position] = monorail;
|
|
}
|
|
|
|
else {
|
|
int place = -1;
|
|
for (int i = position; i < Count; i++) {
|
|
if (_places[i] == null) {
|
|
place = i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (place == -1) return -1;
|
|
|
|
for (int i = place - 1; i >= position; i--) {
|
|
_places[i+1] = _places[i];
|
|
}
|
|
|
|
_places[position] = monorail;
|
|
}
|
|
return position;
|
|
}
|
|
public boolean Remove(int position){
|
|
if (!(position >= 0 && position < Count)) return false;
|
|
|
|
_places[position] = null;
|
|
return true;
|
|
}
|
|
public T Get(int position){
|
|
if (!(position >= 0 && position < Count)) return null;
|
|
|
|
return (T)_places[position];
|
|
}
|
|
}
|