static void Main(string[] args) { var coll = new CollectionType <int>(new int[] { 1, 3, 7, 19, 31, 7, 87 }); WriteLine("Linqed List example:\n\n" + coll.ToString()); coll.RemoveAt(0); // remove 1 coll.AddLast(7); // add 7 to the end coll.AddFirst(2); // add 2 to the begin coll.RemoveAll(7); // remove all 7 coll.AddBefore(30, 3); // add 30 before 31 coll.AddAfter(32, 4); // add 32 after 31 WriteLine("remove the first element, add '7' to the end, add '2' to the begin, remove all '7', add '30' " + "before '31' and '32' after it\n" + coll.ToString()); CollectionType <int> coll2 = new CollectionType <int>(); coll.CopyTo(coll2); coll.Clear(); coll.AddRange(new int[] { 2, 3, 4, 5, 6, 7, 8, 9 }); coll.AddFirst(1); WriteLine("Copy all to another list, clear the old one and fill it again with natural numbers till 9\n" + coll.ToString()); WriteLine("The new list has saved values that were copied, while the old was changed:\n" + coll2.ToString() + "\n\nTask (variant 1):\n"); Triangle[][] tris = new Triangle[4][]; CollectionType <Triangle>[] arr_coll = new CollectionType <Triangle> [4]; Random r = new Random(); int a, b, c, n = 1; for (int i = 0; i < tris.Length; i++) { tris[i] = new Triangle[r.Next(5, 15)]; for (int j = 0; j < tris[i].Length; j++) { a = r.Next(1, 20); b = r.Next(1, 20); c = (int)Sqrt(a * a + b * b - a * b * Cos(r.NextDouble())); if (c == 0) { c = a + b - 1; } try { tris[i][j] = new Triangle(a, b, c); } catch (ArgumentException) { tris[i][j] = new Triangle(3 * n, 4 * n, 5 * n); n++; } } } WriteLine("Non-sorted array:"); foreach (var item in tris) { foreach (var sub in item) { WriteLine(sub.ToString()); } } WriteLine("\n Sorted collections"); for (int i = 0; i < tris.Length; i++) { Array.Sort <Triangle>(tris[i]); WriteLine($"\n collection {i}\n"); foreach (var item in tris[i]) { WriteLine(item.ToString()); } arr_coll[i] = new CollectionType <Triangle>(tris[i]); } WriteLine("\n Enter n"); n = Convert.ToInt32(ReadLine()); // LINQ var n_col = from elem in arr_coll where elem.Length == n select elem; if (n_col.Count() == 0) { WriteLine("There is no collection with such length"); } foreach (var item in n_col) { WriteLine(item.ToString()); } var max = arr_coll.Aggregate((i1, i2) => i1.Length > i2.Length ? i1 : i2); var min = arr_coll.Aggregate((i1, i2) => i1.Length < i2.Length ? i1 : i2); WriteLine("Minimal collection:\n" + min.ToString()); WriteLine("Maximal collection:\n" + max.ToString()); }
public void CopyTo(CollectionType <T> ct) { ct.first = first; ct.Length = Length; }