Advertisement
Gillito

Untitled

Jun 16th, 2015
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication44
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             DynamicArray dynArray = new DynamicArray();
  14.             dynArray.AddToArray(5);
  15.             dynArray.ShowArray();
  16.  
  17.             dynArray.AddToArray(3);
  18.             dynArray.ShowArray();
  19.  
  20.             dynArray.AddToArray(7);
  21.             dynArray.ShowArray();
  22.          
  23.             dynArray.Raverse();
  24.             dynArray.ShowArray();
  25.         }
  26.     }
  27.  
  28.     class DynamicArray
  29.     {
  30.         private int count = 0;
  31.  
  32.         private int[] array = new int[1];
  33.  
  34.         public void AddToArray(int element)
  35.         {
  36.             if (count < array.Length)
  37.                 array[count] = element;
  38.             else
  39.             {
  40.                 int[] tempArray = new int[array.Length * 2];
  41.                 for (int i = 0; i < array.Length; i++)
  42.                     tempArray[i] = array[i];
  43.                 tempArray[count] = element;
  44.                 array = tempArray;
  45.             }
  46.             count++;
  47.         }
  48.  
  49.         public void ShowArray()
  50.         {
  51.             for (int i = 0; i < count; i++)
  52.                 Console.WriteLine(array[i]);
  53.             Console.WriteLine("array.Length " + array.Length);
  54.             Console.WriteLine("Count " + count);
  55.         }
  56.  
  57.         public int[] PublicArray()
  58.         {
  59.             int[] arrayCopy = new int[count];
  60.             for (int i = 0; i < count; i++)
  61.             {
  62.                 arrayCopy[i] = array[i];
  63.             }
  64.             return arrayCopy;
  65.         }
  66.  
  67.         public void ClearArray()
  68.         {
  69.             array = new int[1];
  70.             count = 0;
  71.         }
  72.  
  73.         public void Destroyer(int x)
  74.         {
  75.             for (; x + 1 < count; x++)
  76.                 array[x] = array[x + 1];
  77.             count--;
  78.         }
  79.  
  80.         public void Raverse()
  81.         {
  82.             for (int i = 0; i < count; i++)
  83.                 array[i] = array[count - 1 - i];
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement