Advertisement
k_vychodilova

test_3

May 9th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.80 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 ConsoleApplication28
  8. {
  9.     class Pole<T>
  10.     {
  11.         private List<T> list;
  12.         public int Kapacita { get; private set; }
  13.         public Pole(int kapacita)
  14.         {
  15.             Kapacita = kapacita;
  16.             list = new List<T>(Kapacita);
  17.         }
  18.  
  19.         public void InsertAt(int index, T hodnota)
  20.         {
  21.             list.Insert(index, hodnota);
  22.             Vypis();
  23.         }
  24.  
  25.         public void Vypis()
  26.         {
  27.             Console.WriteLine(string.Join(",", list));
  28.         }
  29.  
  30.     }
  31.  
  32.     class Pole2<T>
  33.     {
  34.         private T[] data;
  35.         public int Kapacita { get; private set; }
  36.         public Pole2(int kapacita)
  37.         {
  38.             Kapacita = kapacita;
  39.             data = new T[Kapacita];
  40.         }
  41.  
  42.         public void InsertAt(int index, T hodnota)
  43.         {
  44.             for (int i = Kapacita - 1; i > index; i--)
  45.             {
  46.                 data[i] = data[i - 1];
  47.             }
  48.             data[index] = hodnota;
  49.             Vypis();
  50.         }
  51.  
  52.         public void Vypis()
  53.         {
  54.             Console.WriteLine(string.Join(",", data));
  55.         }
  56.  
  57.     }
  58.     class Program
  59.     {
  60.         static void Main(string[] args)
  61.         {
  62.  
  63.             // 1. s polem
  64.             // 2. s listem
  65.             Pole<int> p1 = new Pole<int>(4);
  66.             p1.InsertAt(0, 1); // 1
  67.             p1.InsertAt(0, 2); // 2 1
  68.             p1.InsertAt(1, 3); // 2 3 1
  69.             p1.InsertAt(2, 4); // 2 3 4 1
  70.  
  71.             Pole2<int> p2 = new Pole2<int>(4);
  72.             p2.InsertAt(0, 1); // 1
  73.             p2.InsertAt(0, 2); // 2 1
  74.             p2.InsertAt(1, 3); // 2 3 1
  75.             p2.InsertAt(2, 4); // 2 3 4 1
  76.         }  
  77.     }
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement