lolblach333

Untitled

Apr 17th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. namespace WorkingWithArray
  5. {
  6. class Program
  7. {
  8. private static readonly string
  9. InitialArray = "Начальный массив: ",
  10. AfterAdding = "добавления: ",
  11. AfterDeliting = "удаления: ",
  12. SecondlArray = "Второй массив: ",
  13. ElementToRemove = "Элемент для удаления = ",
  14. Empty = "Empty!",
  15. Separator = ", ";
  16.  
  17. static void Main(string[] args)
  18. {
  19. int[] array = new int[0];
  20. int[] secondArray = new int[0];
  21.  
  22. PrintArray(InitialArray, ref array);
  23.  
  24. for (int i = 0; i < 10; i++)
  25. {
  26. AddElement(i, ref array);
  27. }
  28.  
  29. PrintArray(AfterAdding, ref array);
  30.  
  31. int removedElement = array[2];
  32. RemoveElement(2, ref array);
  33. Console.WriteLine(ElementToRemove + removedElement);
  34. PrintArray(AfterDeliting, ref array);
  35. CopyArray(ref array, ref secondArray);
  36. PrintArray(InitialArray, ref array);
  37. PrintArray(SecondlArray, ref array);
  38. Console.ReadKey();
  39. }
  40.  
  41. private static void AddElement(int element, ref int[] array)
  42. {
  43. int[] tempArray = new int[array.Length + 1];
  44.  
  45. for (int i = 0; i < array.Length; i++)
  46. {
  47. tempArray[i] = array[i];
  48. }
  49.  
  50. tempArray[array.Length] = element;
  51. array = tempArray;
  52. }
  53.  
  54. private static void RemoveElement(int index, ref int[] array)
  55. {
  56. int[] tempArray = new int[array.Length - 1];
  57. int arrayIterator = 0;
  58.  
  59. for (int i = 0; i < tempArray.Length; i++)
  60. {
  61. if (index == i)
  62. {
  63. arrayIterator++;
  64. }
  65.  
  66. tempArray[i] = array[arrayIterator];
  67. arrayIterator++;
  68. }
  69.  
  70. array = tempArray;
  71. }
  72.  
  73. private static void CopyArray(ref int[] source, ref int[] destination)
  74. {
  75. destination = source;
  76. }
  77.  
  78. private static void PrintArray(string message, ref int[] array)
  79. {
  80. if (array.Length == 0)
  81. {
  82. Console.WriteLine(message + Empty);
  83. return;
  84. }
  85.  
  86. Console.WriteLine(message + string.Join(Separator, array));
  87. }
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment