Advertisement
Guest User

4.6

a guest
May 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.12 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 ConsoleApp3
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             Console.Title = "4.6 Арсенал";
  15.  
  16.             string[] arr = { "qqq", "www", "eee", "rrr" };
  17.             ShowArray(ref arr);
  18.  
  19.             // Удаление элемента массива
  20.             DelElement(ref arr, 1);
  21.             ShowArray(ref arr);
  22.  
  23.             // Добавление элемента массива
  24.             AddElement(ref arr, "zzz");
  25.             ShowArray(ref arr);
  26.  
  27.             // Перенос одного массива в другой
  28.             string[] newArr = new string[0];
  29.             CopyArray(ref arr, ref newArr);
  30.             ShowArray(ref newArr);
  31.  
  32.             Console.ReadKey();
  33.  
  34.         }
  35.  
  36.         static void DelElement(ref string[] arr, int elemNumber)
  37.         {
  38.             for (int i = elemNumber; i < arr.Length - 1; i++)
  39.             {
  40.                 arr[i] = arr[i + 1];
  41.             }
  42.             NewSize(ref arr, arr.Length - 1);
  43.         }
  44.  
  45.         static void AddElement(ref string[] arr, string s)
  46.         {
  47.             NewSize(ref arr, arr.Length + 1);
  48.  
  49.             for (int i = 0; i < arr.Length; i++)
  50.             {
  51.                 arr[arr.Length - 1] = s;
  52.             }
  53.         }
  54.  
  55.         static void NewSize(ref string[] arr, int size)
  56.         {
  57.             string[] arrTmp = new string[size];
  58.             for (int i = 0; i < Math.Min(arr.Length, size); i++)
  59.             {
  60.                 arrTmp[i] = arr[i];
  61.             }
  62.             arr = arrTmp;
  63.         }
  64.  
  65.         static void CopyArray(ref string[] arrFrom, ref string[] arrTo)
  66.         {
  67.             NewSize(ref arrTo, arrFrom.Length);
  68.             for (int i = 0; i < arrFrom.Length; i++)
  69.                 arrTo[i] = arrFrom[i];
  70.         }
  71.  
  72.         static void ShowArray(ref string[] arr)
  73.         {
  74.             for (int i = 0; i < arr.Length; i++)
  75.                 Console.WriteLine(arr[i]);
  76.             Console.WriteLine();
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement