Viktomirova

ImplementArrayList

Nov 5th, 2021 (edited)
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.62 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ImplementArrayList
  6. {
  7.     public class CustomArrayList
  8.     {
  9.         private object[] arrayObject;
  10.  
  11.         private int count;
  12.  
  13.         public int Count
  14.         {
  15.             get => count;
  16.             set
  17.             {
  18.                 count = value;
  19.             }
  20.         }
  21.  
  22.         private static readonly int INITIAL_CAPACITY = 4;
  23.  
  24.         public CustomArrayList()
  25.         {
  26.             arrayObject = new object[INITIAL_CAPACITY];
  27.             count = 0;
  28.         }
  29.  
  30.         public void Add(object item)
  31.         {
  32.             Insert(count, item);
  33.         }
  34.  
  35.         public bool Contains(object item)
  36.         {
  37.             int currentIndex = IndexOf(item);
  38.  
  39.             if (currentIndex == -1)
  40.             {
  41.                 return false;
  42.             }
  43.  
  44.             return true;
  45.         }
  46.  
  47.         public object Remove(int index)
  48.         {
  49.             if (index >= count || index < 0)
  50.             {
  51.                 throw new ArgumentOutOfRangeException("Invalid index: " + index);
  52.             }
  53.  
  54.             object item = arrayObject[index];
  55.  
  56.             arrayObject[index] = null;
  57.  
  58.             for (int i = index; i < arrayObject.Length - 1; i++)
  59.             {
  60.                 arrayObject[i] = arrayObject[i + 1];
  61.             }
  62.  
  63.             arrayObject[count - 1] = null;
  64.             count--;
  65.  
  66.             if (count <= arrayObject.Length / 2)
  67.             {
  68.                 Shrink();
  69.             }
  70.  
  71.             return item;
  72.         }
  73.  
  74.         private void Shrink()
  75.         {
  76.             object[] copy = new object[arrayObject.Length / 2];
  77.             Array.Copy(arrayObject, copy, copy.Length);
  78.             arrayObject = copy;
  79.         }
  80.  
  81.         public int Remove(object item)
  82.         {
  83.             int index = IndexOf(item);
  84.             if (index == -1)
  85.             {
  86.                 return index;
  87.             }
  88.  
  89.             Remove(index);
  90.  
  91.             return index;
  92.         }
  93.  
  94.         public void Insert(int index, object item)
  95.         {
  96.             if (count == arrayObject.Length)
  97.             {
  98.                 Resize();
  99.             }
  100.  
  101.             for (int i = arrayObject.Length - 1; i > index; i--)
  102.             {
  103.                 arrayObject[i] = arrayObject[i - 1];
  104.             }
  105.  
  106.             arrayObject[index] = item;
  107.             count++;
  108.         }
  109.  
  110.         public int IndexOf(object item)
  111.         {
  112.             for (int i = 0; i < arrayObject.Length; i++)
  113.             {
  114.                 if (arrayObject[i].Equals(item))
  115.                 {
  116.                     return i;
  117.                 }
  118.             }
  119.            
  120.             return -1;
  121.         }
  122.  
  123.         public void Clear()
  124.         {
  125.             arrayObject = new object[INITIAL_CAPACITY];
  126.             count = 0;
  127.         }
  128.  
  129.         public object this[int index]
  130.         {
  131.             get
  132.             {
  133.                 if (index >= count || index < 0)
  134.                 {
  135.                     throw new ArgumentOutOfRangeException("Invalid index: " + index);
  136.                 }
  137.  
  138.                 return arrayObject[index];
  139.             }
  140.             set
  141.             {
  142.                 if (index >= count || index < 0)
  143.                 {
  144.                     throw new ArgumentOutOfRangeException("Invalid index: " + index);
  145.                 }
  146.  
  147.                 arrayObject[index] = value;
  148.             }
  149.         }
  150.  
  151.         public void Resize()
  152.         {
  153.             object[] copy = new object[arrayObject.Length * 2];
  154.             Array.Copy(arrayObject, copy, arrayObject.Length);
  155.             arrayObject = copy;
  156.         }
  157.     }
  158. }
  159.  
Add Comment
Please, Sign In to add comment