Готовая лабораторная работа 3
This commit is contained in:
parent
4de1bca002
commit
0a9f5bcbb6
@ -1,4 +1,5 @@
|
||||
namespace HoistingCrane.CollectionGenericObjects
|
||||
using System;
|
||||
namespace HoistingCrane.CollectionGenericObjects
|
||||
{
|
||||
public class MassivGenericObjects<T> : ICollectionGenericObjects<T> where T : class
|
||||
{
|
||||
@ -17,64 +18,67 @@
|
||||
{
|
||||
if (value > 0)
|
||||
{
|
||||
arr = new T?[value];
|
||||
if (arr.Length > 0)
|
||||
{
|
||||
Array.Resize(ref arr, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
arr = new T?[value];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
public T? Get(int position)
|
||||
{
|
||||
if(position >= 0 && position < arr.Length)
|
||||
if (position >= 0 && position < arr.Length)
|
||||
{
|
||||
return arr[position];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public int Insert(T obj)
|
||||
{
|
||||
return Insert(obj, 0);
|
||||
}
|
||||
|
||||
public int Insert(T obj, int position)
|
||||
{
|
||||
|
||||
//todo Проверка позиции
|
||||
if (position < 0 || position > Count)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int pos = position - 1;
|
||||
|
||||
while (position < Count)
|
||||
if (arr[position] == null)
|
||||
{
|
||||
if (arr[position] == null)
|
||||
arr[position] = obj;
|
||||
return position;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Insert(obj, position + 1) != -1)
|
||||
{
|
||||
arr[position] = obj;
|
||||
return position;
|
||||
}
|
||||
position++;
|
||||
}
|
||||
while (pos > 0)
|
||||
{
|
||||
if (arr[position] == null)
|
||||
if (Insert(obj, position - 1) != -1)
|
||||
{
|
||||
arr[position] = obj;
|
||||
return position;
|
||||
}
|
||||
position--;
|
||||
}
|
||||
|
||||
return -1;
|
||||
|
||||
}
|
||||
|
||||
public T? Remove(int position)
|
||||
{
|
||||
if (position < 0 || position > Count)
|
||||
if (position >= 0 && position < Count)
|
||||
{
|
||||
return null;
|
||||
T? temp = arr[position];
|
||||
arr[position] = null;
|
||||
return temp;
|
||||
}
|
||||
|
||||
T? removed_object = arr[position];
|
||||
arr[position] = null;
|
||||
return removed_object;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user