Advertisement
simonradev

ResizableArray

Apr 5th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. namespace ResizablArray
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class ResizablArray
  8.     {
  9.         public static int currentSizeOfArray = 4;
  10.         public static int currIndexOfElement = 0;
  11.         public static int[] array = new int[4];
  12.  
  13.         public static void Main()
  14.         {
  15.             string inputLine = ReadInputFromConsole();
  16.             while (inputLine != "end")
  17.             {
  18.                 string[] commandInfo = inputLine.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  19.  
  20.                 switch (commandInfo[0])
  21.                 {
  22.                     case "push":
  23.                         int number = int.Parse(commandInfo[1]);
  24.  
  25.                         PushElementInTheEndOfTheArray(number);
  26.                         break;
  27.  
  28.                     case "pop":
  29.                         RemoveElementAtIndex(array.Length - 1);
  30.                         break;
  31.  
  32.                     case "removeat":
  33.                         int index = int.Parse(commandInfo[1]);
  34.  
  35.                         RemoveElementAtIndex(index);
  36.                         break;
  37.  
  38.                     case "clear":
  39.                         ClearTheArray();
  40.                         break;
  41.  
  42.                     default:
  43.                         break;
  44.                 }
  45.  
  46.                 inputLine = ReadInputFromConsole();
  47.             }
  48.  
  49.             PrintTheElements();
  50.         }
  51.  
  52.         private static void PrintTheElements()
  53.         {
  54.             StringBuilder result = new StringBuilder();
  55.             if (currIndexOfElement == 0)
  56.             {
  57.                 result.Append("empty array");
  58.  
  59.                 goto Print;
  60.             }
  61.  
  62.             for (int currElement = 0; currElement < currIndexOfElement; currElement++)
  63.             {
  64.                 result.Append(array[currElement]);
  65.  
  66.                 if (currElement + 1 != currIndexOfElement)
  67.                 {
  68.                     result.Append(" ");
  69.                 }
  70.             }
  71.  
  72.             Print:
  73.             Console.WriteLine(result.ToString());
  74.         }
  75.  
  76.         private static void ClearTheArray()
  77.         {
  78.             currentSizeOfArray = 4;
  79.             currIndexOfElement = 0;
  80.  
  81.             array = new int[currentSizeOfArray];
  82.         }
  83.  
  84.         private static void RemoveElementAtIndex(int index)
  85.         {
  86.             if (array.Length == 0 || index < 0 || index >= array.Length)
  87.             {
  88.                 return;
  89.             }
  90.  
  91.             for (int currElement = index; currElement < array.Length - 1; currElement++)
  92.             {
  93.                 array[currElement] = array[currElement + 1];
  94.             }
  95.  
  96.             array[array.Length - 1] = 0;
  97.  
  98.             currIndexOfElement--;
  99.         }
  100.  
  101.         private static void PushElementInTheEndOfTheArray(int numberToAdd)
  102.         {
  103.             if (currIndexOfElement == array.Length)
  104.             {
  105.                 DoubleUpTheSizeOfTheArray();
  106.             }
  107.  
  108.             array[currIndexOfElement] = numberToAdd;
  109.  
  110.             currIndexOfElement++;
  111.         }
  112.  
  113.         private static void DoubleUpTheSizeOfTheArray()
  114.         {
  115.             int[] arrayHolder = array.ToArray();
  116.  
  117.             currentSizeOfArray *= 2;
  118.             array = new int[currentSizeOfArray];
  119.  
  120.             for (int currElement = 0; currElement < arrayHolder.Length; currElement++)
  121.             {
  122.                 array[currElement] = arrayHolder[currElement];
  123.             }
  124.         }
  125.  
  126.         private static string ReadInputFromConsole()
  127.         {
  128.             return Console.ReadLine().Trim().ToLower();
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement