PIbd-14 ChertovA.A. LabWork3 Simple #3

Closed
pibd14chertovandrey wants to merge 2 commits from lab03 into lab02
2 changed files with 133 additions and 0 deletions
Showing only changes of commit 789d187fb7 - Show all commits

View File

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRoadTrain.CollectionGenericObjects
{
internal interface ICollectionGenericObjects
{
}
}

View File

@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ProjectRoadTrain.CollectionGenericObjects;
public class MassiveGenericObjects<T> : ICollectionGenericObjects<T>
where T : class
{
private T?[] _collection;
public int Count => _collection.Length;
public int SetMaxCount
{
set
{
if (value > 0)
{
if (_collection.Length > 0)
{
Array.Resize(ref _collection, value);
}
else
{
_collection = new T?[value];
}
}
}
}
public MassiveGenericObjects()
{
_collection = Array.Empty<T?>();
}
public T? Get(int position)
{
if (position >= 0 && position < Count)
{
return _collection[position];
}
return null;
}
public int Insert(T obj)
{
for (int i = 0; i < Count; i++)
Review

Правильнее было вызвать Insert(T obj, 0);

Правильнее было вызвать Insert(T obj, 0);
{
if (_collection[i] == null)
{
_collection[i] = obj;
return i;
}
}
return -1;
}
public int Insert(T obj, int position)
{
if (position < 0 || position >= Count)
{
return -1;
}
if (_collection[position] != null)
{
bool pushed = false;
for (int index = position + 1; index < Count; index++)
{
if (_collection[index] == null)
{
position = index;
pushed = true;
break;
}
}
if (!pushed)
{
for (int index = position - 1; index >= 0; index--)
{
if (_collection[index] == null)
{
position = index;
pushed = true;
break;
}
}
}
if (!pushed)
{
return position;
}
}
_collection[position] = obj;
return position;
}
public T? Remove(int position)
{
if (position < 0 || position >= Count)
{
return null;
}
if (_collection[position] == null) return null;
T? temp = _collection[position];
_collection[position] = null;
return temp;
}
}