PIbd21.LyovushkinaA.A.Conta.../SetGeneric.java
2023-10-24 01:06:14 +04:00

119 lines
2.9 KiB
Java

public class SetGeneric<T extends Object>
{
private Object[] _places;
public int Count;
public SetGeneric(int count)
{
_places = new Object[count];
Count = _places.length;
}
public int Insert(T ship)
{
// TODO вставка в начало набора
int temp = 0;
int k = 0;
for(int j = 0; j < _places.length; j++) {
if (_places[j] != null)
{
k++;
}
}
if (k == _places.length)
{
return -1;
}
else
{
for (int i = 0; i < _places.length; i++)
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > 0; i--)
{
_places[i] = _places[i -1];
}
_places[0] = ship;
return 0;
}
}
public boolean Insert(T ship, int position)
{
int temp = 0;
int k = 0;
for (int j = position; j < _places.length; j++)
{
if (_places[j] != null)
{
k++;
}
}
if (position < _places.length && k < _places.length-position)
{
for (int i = position; i < _places.length; i++)
{
if (_places[i] == null)
{
temp = i;
break;
}
}
for (int i = temp; i > position; i--)
{
_places[i] = _places[i - 1];
}
_places[position] = ship;
return true;
}
else
{
return false;
}
}
public boolean Remove(int position)
{
// TODO проверка позиции
// TODO удаление объекта из массива, присвоив элементу массива
//значение null
if(position < _places.length)
{
_places[position] = null;
return true;
}
else
{
return false;
}
}
public T Get(int position)
{
// TODO проверка позиции
if(position < _places.length)
{
return (T)_places[position];
}
else
{
return null;
}
}
}