-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnCollection.cs
119 lines (108 loc) · 2.54 KB
/
OwnCollection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System.Collections;
using System.Collections.ObjectModel;
namespace ConsoleApp;
public class OwnCollection<T>: IEnumerable, IEnumerator
{
private T[] array;
private int position = -1;
public T this[int index]
{
get => array[index];
set => array[index] = value;
}
public OwnCollection(params T[] array) => this.array = array;
public OwnCollection()
{
array = Array.Empty<T>();
}
public void Reset() => position = -1;
public object Current
{
get
{
if (position == -1 || position >= array.Length)
throw new ArgumentException();
return array[position];
}
}
public IEnumerator GetEnumerator()
{
return array.GetEnumerator();
}
public bool MoveNext()
{
if (position < array.Length - 1)
{
position++;
return true;
}
else
return false;
}
public void Append(T item)
{
int n = array.Length + 1;
T[] arrayCopy = new T [n];
for (int i = 0; i < n - 1; i++)
{
arrayCopy[i] = array[i];
}
arrayCopy[n - 1] = item;
array = arrayCopy;
}
public int Length() => array.Length;
public void NewCopy(ref OwnCollection<T> ownCollection)
{
array = new T[ownCollection.Length()];
T item;
for (int i = 0; i < array.Length; i++)
{
/*ownCollection.MoveNext();
item = (T)ownCollection.Current;*/
array[i] = ownCollection[i];
}
}
public bool SearchItem(T item)
{
foreach (T i in array)
{
if (i.Equals(item))
{
return true;
}
}
return false;
}
public void Pop()
{
int n = this.array.Length - 1;
T[] array = new T[n];
for (int i = 0; i < n; i++)
{
array[i] = this.array[i];
}
this.array = array;
}
public void DeleteItem(T item)
{
int count = 0;
foreach (var i in this.array)
{
if (i.Equals(item))
{
count++;
}
}
T[] array = new T[this.array.Length-count];
int index = 0;
for (int i = 0; i < this.array.Length; i++)
{
if (!item.Equals(this.array[i]))
{
array[index] = this.array[i];
index++;
}
}
this.array = array;
}
}