static void Main(string[] args) { var listtt = new List<int>(); GenericList<int> myList = new GenericList<int>(10); //ADD myList.Add(1); myList.Add(2); myList.Add(3); Console.WriteLine(myList.ToString()); Console.WriteLine("Insert element"); //INSERT AT GIVEN POSITION myList.Insert(3, 50); myList.Insert(3, 60); Console.WriteLine(myList.ToString()); Console.WriteLine("Find element:"); //FIND CERTAINT ELEMENT AT WHAT INDEXES EXISTS myList.Find(25); Console.WriteLine("Remove element:"); //REMOVE AT CERTAIN INDEX myList.Remove(2); Console.WriteLine(myList.ToString()); Console.WriteLine("Find maximum"); //FIND MAX Console.WriteLine(myList.Max()); Console.WriteLine("Find minimum"); //FIND MIN Console.WriteLine(myList.Max()); //CLEAR ALL Console.WriteLine("Clear all"); myList.Clear(); Console.WriteLine(myList.ToString()); }
static void Main(string[] args) { MyCollection<int> intCollection = new MyCollection<int>(2, 6); intCollection.Add(3); for(int i=0;i<intCollection.Count();i++) Console.WriteLine(intCollection[i]); intCollection.Sort(); Console.WriteLine("После сортировки:"); for (int i = 0; i < intCollection.Count(); i++) Console.WriteLine(intCollection[i]); int newInt = intCollection.CreateEntity(); Console.WriteLine("создали число "+newInt); //коллекция котов MyCollection<Cat> catCollection = new MyCollection<Cat>(); catCollection.Add(new Cat { Name = "Мурка", Weight = 8 }); catCollection.Add(new Cat { Name = "Мурзик", Weight = 15 }); catCollection.Add(new Cat { Name = "Барсик", Weight = 10 }); for (int i = 0; i < catCollection.Count(); i++) Console.WriteLine(catCollection[i]); catCollection.Sort(); Console.WriteLine("После сортировки:"); for (int i = 0; i < catCollection.Count(); i++) Console.WriteLine(catCollection[i]); foreach (Cat cat in catCollection) Console.WriteLine(cat); Cat newCat = catCollection.CreateEntity(); Console.WriteLine("Создали кота "+newCat); DateTime time1 = DateTime.Now; ArrayList arrList = new ArrayList(); for (int i = 0; i < 1000000;i++ ) { arrList.Add(i);//упаковка int x =(int) arrList[i];//распаковка } Console.WriteLine((DateTime.Now-time1).TotalSeconds); arrList.Add("Вася"); DateTime time2 = DateTime.Now; List<int> list = new List<int>(); for (int i = 0; i < 1000000; i++) { list.Add(i); int x = list[i]; } //list.Add("Вася"); Console.WriteLine((DateTime.Now - time2).TotalSeconds); Console.ReadKey(); }
public void GenericListDemo() { List<Product> listOfProducts = new List<Product>() { new Product(001, "mouse", 12), new Product(002, "keyboard", 20), new Product(003, "pencil", 1.23) }; Product p1 = new Product(000, "headset", 19.45); listOfProducts.Insert(1, p1); foreach (Product item in listOfProducts) { Console.WriteLine(item); } Product[] pArr = listOfProducts.ToArray(); }
// We will get the shortest path between the begin RicePaddy and the end RicePaddy by Dijkstra's algorithm. Dictionary<RicePaddy, Route> CalculateShortestRoutes(RicePaddy start) { var shortestRoutes = new Dictionary<RicePaddy, Route>(); var handled = new List<RicePaddy>(); foreach (var ricePaddy in m_ricePaddies) { shortestRoutes.Add(ricePaddy, new Route(ricePaddy.Identifier)); } shortestRoutes[start].TotalDistance = 0; while (handled.Count != m_ricePaddies.Count) { var shortestRicePaddies = shortestRoutes.OrderBy(_ => _.Value.TotalDistance).Select(_ => _.Key).ToArray(); var processing = default(RicePaddy); foreach (var ricePaddy in shortestRicePaddies) { if (!handled.Contains(ricePaddy)) { if (shortestRoutes[ricePaddy].TotalDistance == int.MaxValue) return shortestRoutes; processing = ricePaddy; break; } } var selectedRoads = m_roads.Where(_ => _.A == processing); foreach (var road in selectedRoads) { if (shortestRoutes[road.B].TotalDistance > road.Distance + shortestRoutes[road.A].TotalDistance) { var roads = shortestRoutes[road.A].Roads.ToList(); roads.Add(road); shortestRoutes[road.B].Roads = roads; shortestRoutes[road.B].TotalDistance = road.Distance + shortestRoutes[road.A].TotalDistance; } } handled.Add(processing); } return shortestRoutes; }
private static List<string> GetNamesOfCodeFilesInProjectDirectory() { string path = Directory.GetCurrentDirectory(); string[] files = Directory.GetFiles(path + @"\..\..", "*.cs"); var fileNames = new List<string>(); foreach (var s in files) { FileInfo fileInfo = new FileInfo(s); if (!fileInfo.Name.Equals("Program.cs")) fileNames.Add(fileInfo.Name.Substring(0, fileInfo.Name.Length - 3)); } fileNames.Sort(); return fileNames; }
private static int DisplayMenuChoices(List<string> codeFiles) { while (true) { Console.Clear(); Console.WriteLine("\n Please choose a number between 1 and {0}, or enter 'q' to quit:\n", codeFiles.Count); var i = 1; foreach (string s in codeFiles) { Console.WriteLine("{0, 4}. {1}", i, s); } Console.Write("\n > "); var input = Console.ReadLine(); int choice; if (Int32.TryParse(input, out choice) && choice >= 1 && choice <= codeFiles.Count) { Console.Clear(); return choice - 1; } if (input.ToLower().Equals("q")) Environment.Exit(0); } }
public void WorkingWithGenerics() { Console.WriteLine("\nGenerics"); GenericEmployeeCollection<GenericEmployeeType> newobj = new GenericEmployeeCollection<GenericEmployeeType>(); GenericEmployeeType emp1 = new GenericEmployeeType(90, "naynish", 65000); GenericEmployeeType emp2 = new GenericEmployeeType(91, "tripti", 75000); GenericEmployeeType emp3 = new GenericEmployeeType(92, "jay", 85000); GenericEmployeeType emp4 = new GenericEmployeeType(93, "purab", 95000); newobj.myGenDictionary.Add(emp1.Id, emp1); newobj.myGenDictionary.Add(emp2.Id, emp2); newobj.myGenDictionary.Add(emp3.Id, emp3); newobj.myGenDictionary.Add(emp4.Id, emp4); foreach (KeyValuePair<Int32, GenericEmployeeType> item in newobj.myGenDictionary) { Console.WriteLine(item.Key + " " + item); } Console.WriteLine("\nGeneric List"); GenericEmployeeType[] empArr = new GenericEmployeeType[] { emp1, emp2, emp3, emp4 }; newobj.ShowListEntries<GenericEmployeeType>(empArr); Console.WriteLine("\nIncentives: Specifying type parameters for members"); emp1.Incentives<Int32>(90); Fruit[] fruit = new Fruit[] { new Fruit("apple", 45), new Fruit("watermelon", 12), new Fruit("peach", 78), new Fruit("banana", 10), }; System.Array.Sort<Fruit>(fruit, new ImplementIComparer()); foreach (Fruit item in fruit) { Console.WriteLine(item); } System.Array.Sort<Fruit>(fruit, Fruit.SortByPrice); //sorting by price Console.WriteLine("\n sorting fruits by price"); foreach (Fruit item in fruit) { Console.WriteLine(item); } //implementing IComparer<OperatingSystems> in the same class //collection initialization syntax //Note You can only apply collection initialization syntax to classes that support an Add() method, which is //formalized by the ICollection<T>/ICollection interfaces. List<OperatingSystems> li = new List<OperatingSystems>() { new OperatingSystems("WIN32", 900), new OperatingSystems("OSX", 906), new OperatingSystems("LINUX", 400), new OperatingSystems("UBUNTU", 590) }; li.Sort(new OperatingSystems()); foreach (OperatingSystems item in li) { Console.WriteLine(item); } }