69 lines
1.9 KiB
Java
69 lines
1.9 KiB
Java
|
package Trolleybus;
|
|||
|
|
|||
|
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 bus){
|
|||
|
return Insert(bus, 0);
|
|||
|
}
|
|||
|
//Вставка на какую-то позицию
|
|||
|
public int Insert(T bus, int position)
|
|||
|
{
|
|||
|
if (position >= Count || position < 0)
|
|||
|
{
|
|||
|
return -1;
|
|||
|
}
|
|||
|
|
|||
|
if (_places[position] == null)
|
|||
|
{
|
|||
|
_places[position] = bus;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
//проверка, что в массиве после вставляемого эл-а есть место
|
|||
|
int index = position;
|
|||
|
while (_places[index] != null)
|
|||
|
{
|
|||
|
index++;
|
|||
|
if (index >= Count)
|
|||
|
{
|
|||
|
//места в массиве нет, т.е. ни по какому индексу вставить нельзя
|
|||
|
return -1;
|
|||
|
}
|
|||
|
}
|
|||
|
for (int i = index; i > position; i--)
|
|||
|
{
|
|||
|
_places[i] = _places[i - 1];
|
|||
|
|
|||
|
}
|
|||
|
//вставка по позиции
|
|||
|
_places[position] = bus;
|
|||
|
|
|||
|
}
|
|||
|
//индекс в массиве, по которому вставили, т.е. вставка прошла успешно
|
|||
|
return position;
|
|||
|
}
|
|||
|
public boolean Remove(int position)
|
|||
|
{
|
|||
|
if (position >= Count || position < 0)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
_places[position] = null;
|
|||
|
return true;
|
|||
|
}
|
|||
|
public T Get(int position)
|
|||
|
{
|
|||
|
if (position >= Count || position < 0)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return (T)_places[position];
|
|||
|
}
|
|||
|
}
|