PIbd-14_Antonova_A.A.__Hard/CollectionGenericObjects/MassiveGenericObjects.java

86 lines
1.9 KiB
Java
Raw Permalink Normal View History

2024-06-10 13:56:47 +04:00
package CollectionGenericObjects;
import java.lang.reflect.Array;
public class MassiveGenericObjects<T> implements ICollectionGenericObjects<T> {
private T[] _collection;
int Count;
@Override
public int getCount() {
return _collection.length;
}
@Override
public void SetMaxCount(int count, Class<T> type) {
if (count > 0) {
_collection = (T[]) Array.newInstance(type, count);
Count = count;
}
}
@Override
public T Get(int position) {
if (position >= getCount() || position < 0)
return null;
return (T) _collection[position];
}
@Override
public int Insert(T obj) {
for (int i = 0; i < _collection.length; ++i)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
return -1;
}
@Override
public int Insert(T obj, int position) {
if (position < 0 || position > Count)
{
return -1;
}
if (_collection[position] == null)
{
_collection[position] = obj;
return position;
}
for (int i = position + 1; i < _collection.length; ++i)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return position;
}
}
for (int i = 0; i < position; ++i)
{
if (_collection[i] == null)
{
_collection[i] = obj;
return position;
}
}
return -1;
}
@Override
public T Remove(int position) {
if (position >= 0 && position < Count)
{
T remove = _collection[position];
_collection[position]= null;
return remove;
}
return null;
}
}