Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- public class GenericClass<T>
- {
- private T[] arr;
- private int count = 0;
- //constructor
- public GenericClass(int capacity)
- {
- this.arr = new T[capacity];
- }
- //methods
- public void StrechArray()
- {
- T[] tempArr = new T[arr.Length * 2];
- Array.Copy(arr, 0, tempArr, 0, arr.Length);
- arr = tempArr;
- }
- public void AddElements(T elem)
- {
- if (count < arr.Length)
- {
- arr[count] = elem;
- count++;
- }
- else
- {
- StrechArray();
- arr[count] = elem;
- count++;
- }
- }
- public T AccesElement(int index)
- {
- if (index <= count)
- {
- return arr[index];
- }
- else
- {
- throw new IndexOutOfRangeException("The index is outsite the array!");
- }
- }
- public void DeleteElement(int index)
- {
- if (index <= count)
- {
- T[] tempArr = new T[arr.Length];
- int tempArrindex = 0;
- for (int fromBegToIndex = 0; fromBegToIndex < index; fromBegToIndex++, tempArrindex++)
- {
- tempArr[tempArrindex] = arr[fromBegToIndex];
- }
- for (int toTheEnd = index + 1; toTheEnd < count; toTheEnd++, tempArrindex++) //moje i do count
- {
- tempArr[tempArrindex] = arr[toTheEnd];
- }
- Array.Copy(tempArr, 0, arr, 0, arr.Length);
- arr = tempArr;
- count--;
- }
- else
- {
- throw new IndexOutOfRangeException("The index is outsite the array!");
- }
- }
- public void InsertElement(int index, T element)
- {
- T[] tempArr = new T[arr.Length];
- int tempArrIndex = 0;
- if (count + 1 < arr.Length)
- {
- for (int i = 0; i < index; i++, tempArrIndex++)
- {
- tempArr[tempArrIndex] = arr[i];
- }
- tempArr[tempArrIndex] = element;
- tempArrIndex++;
- count++;
- for (int toTheEnd = index; toTheEnd < count; toTheEnd++, tempArrIndex++)
- {
- tempArr[tempArrIndex] = arr[toTheEnd];
- }
- Array.Copy(tempArr, 0, arr, 0, arr.Length);
- arr = tempArr;
- }
- else
- {
- StrechArray();
- InsertElement(index, element);
- }
- }
- public void ClearArr()
- {
- for (int i = 0; i < count; )
- {
- DeleteElement(i);
- }
- }
- public int FindIndexByElement(T element)
- {
- int index = Array.IndexOf(arr, element);
- return index;
- }
- public override string ToString()
- {
- StringBuilder allElements = new StringBuilder();
- for (int i = 0; i < count; i++)
- {
- allElements.AppendFormat("{0,10} is the {1} element of the array",arr[i].ToString(),(i+1));
- allElements.AppendLine();
- }
- string final = allElements.ToString();
- return final;
- }
- //adding Max and Min methods
- public T Max<T>() where T : System.IComparable<T>, IComparable
- {
- dynamic max = arr[0];
- for (int i = 0; i < count; i++)
- {
- T tempValue = (dynamic)this.arr[i];
- if (tempValue.CompareTo(max)>0)
- {
- max = arr[i];
- }
- }
- return max;
- }
- public T Min<T>() where T : System.IComparable<T>, IComparable
- {
- dynamic min = arr[0];
- for (int i = 0; i < count; i++)
- {
- T tempValue = (dynamic)this.arr[i];
- if (tempValue.CompareTo(min) < 0)
- {
- min = arr[i];
- }
- }
- return min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement