Advertisement
desislava_topuzakova

CustomArrayList.cs

May 30th, 2020
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.70 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Runtime.Serialization;
  4. using System.Text;
  5.  
  6. namespace CustomArrayList
  7. {
  8.     class CustomArrayList
  9.     {
  10.         //елементи, брой елементи, капацитет
  11.         private object[] elements;
  12.         private int count;
  13.         private static int CAPACITY = 4;
  14.  
  15.         public CustomArrayList()
  16.         {
  17.             elements = new object[CAPACITY];
  18.             count = 0;
  19.         }
  20.  
  21.         public void Add(object item)
  22.         {
  23.             elements[count] = item;
  24.             count++;
  25.         }
  26.  
  27.         public void Insert(int index, object item)
  28.         {
  29.             elements[index] = item;
  30.             count++;
  31.         }
  32.  
  33.         public int IndexOf(object item)
  34.         {
  35.             for (int i = 0; i < elements.Length; i++)
  36.             {
  37.                 object currentElement = elements[i];
  38.                 if (item.Equals(currentElement))
  39.                 {
  40.                     return i;
  41.                 }
  42.             }
  43.  
  44.             throw new Exception("Element not found");
  45.         }
  46.  
  47.         public void Clear()
  48.         {
  49.             Array.Clear(elements, 0, elements.Length);
  50.         }
  51.  
  52.         public bool Contains(object item)
  53.         {
  54.             for (int i = 0; i < elements.Length; i++)
  55.             {
  56.                 object currentElement = elements[i];
  57.                 if (item.Equals(currentElement))
  58.                 {
  59.                     return true;
  60.                 }
  61.             }
  62.             return false;
  63.         }
  64.  
  65.         public override string ToString()
  66.         {
  67.            
  68.             return string.Join(", ", elements) + "\nCount: "+ count;
  69.         }
  70.     }
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement