public ArrayList_p() { arrayList = new ArrayList(); arrayList1 = new ArrayList(40); Console.WriteLine("ArrayList default capacity: {0}", arrayList.Capacity); Console.WriteLine("ArrayList(40) default capacity: {0}", arrayList1.Capacity); arrayList.Add("some"); arrayList.Add("text"); Console.WriteLine("ArrayList capacity (after adding 2 elements): {0}", arrayList.Capacity); arrayList.Add("foo"); arrayList.Add("bar"); arrayList.Add("lol"); Console.WriteLine("ArrayList capacity (after adding 3 elements): {0}", arrayList.Capacity); Console.WriteLine("ArrayList count elements: {0}", arrayList.Count); Console.WriteLine("ArrayList has a fixed size: {0}", arrayList.IsFixedSize); Console.WriteLine("ArrayList is read-only: {0}", arrayList.IsReadOnly); Console.WriteLine("ArrayList is synchronized (thread-safe): {0}", arrayList.IsSynchronized); var s_arrList = ArrayList.Synchronized(arrayList); Console.WriteLine("ArrayList is synchronized (thread-safe): {0}", s_arrList.IsSynchronized); lock (arrayList.SyncRoot) { foreach (var element in arrayList) { Console.Write("{0} ", element.ToString()); } Console.Write("\n"); } // ArrayList does not guarantee sorted array, need to sort in manually before doing BinarySearch arrayList.Sort(); int elementIndex = arrayList.BinarySearch("foo"); Console.WriteLine("ArrayList BinarySearch returns element position: {0}", elementIndex.ToString()); arrayList.TrimToSize(); Console.WriteLine("ArrayList capacity after trimming to size: {0}", arrayList.Capacity); arrayList.Clear(); Console.WriteLine("ArrayList count after clearing: {0}", arrayList.Count); Console.WriteLine("ArrayList capacity after clearing: {0}", arrayList.Capacity); }
static void Main(string[] args) { Circle objectForSearch = new Circle(2.4, 3.7, 6.9); Circle result = new Circle(); Stopwatch stopWatch = new Stopwatch(); Random rand = new Random(); ArrayList array = new ArrayList(); LinkedList <Circle> linkedList = new LinkedList <Circle>(); Stack <Circle> stack = new Stack <Circle>(); Queue <Circle> queue = new Queue <Circle>(); Hashtable hashTable = new Hashtable(); Dictionary <int, Circle> dictionary = new Dictionary <int, Circle>(); /*----------------------------------------------------------------------------------------- * ADD ELEMENT -- ArrayList vs LinkedList * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); for (int i = 0; i < 500000; i++) { array.Add(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } array.Add(objectForSearch); for (int i = 0; i < 500000; i++) { array.Add(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements - RunTime for ArrayList - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < 500000; i++) { linkedList.AddLast(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } linkedList.AddLast(objectForSearch); for (int i = 0; i < 500000; i++) { linkedList.AddLast(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements RunTime for LinkedList - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * SEARCH ELEMENT -- ArrayList vs LinkedList * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); array.Sort(new CircleComparer()); int index = array.BinarySearch((object)objectForSearch, new CircleComparer()); result = (Circle)array[index]; stopWatch.Stop(); Console.WriteLine("\nSearch Elements RunTime for ArrayList - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); result = (Circle)linkedList.Find(objectForSearch).Value; stopWatch.Stop(); Console.WriteLine("Search Elements RunTime for LinkedList - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * REMOVE ELEMENT -- ArrayList vs LinkedList * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); array.Remove(objectForSearch); stopWatch.Stop(); Console.WriteLine("\nRemove Elements RunTime for ArrayList - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); linkedList.Remove(objectForSearch); stopWatch.Stop(); Console.WriteLine("Remove Elements RunTime for LinkedList - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * ADD ELEMENT -- Stack vs Queue * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); for (int i = 0; i < 5000; i++) { stack.Push(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stack.Push(objectForSearch); for (int i = 0; i < 5000; i++) { stack.Push(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("\nAdd Elements - RunTime for Stack - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < 5000; i++) { queue.Enqueue(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } queue.Enqueue(objectForSearch); for (int i = 0; i < 5000; i++) { queue.Enqueue(new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements - RunTime for Queue - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * SEARCH ELEMENT -- Stack vs Queue * ------------------------------------------------------------------------------------------*/ CircleComparer circleComparer = new CircleComparer(); stopWatch.Start(); for (int i = 0; i < 10000; i++) { if (circleComparer.Compare(objectForSearch, stack.ElementAt(i)) == 0) { result = stack.ElementAt(i); } } stopWatch.Stop(); Console.WriteLine("\nSearch Elements - RunTime for Stack - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < 10000; i++) { if (circleComparer.Compare(objectForSearch, queue.ElementAt(i)) == 0) { result = queue.ElementAt(i); } } stopWatch.Stop(); Console.WriteLine("Search Elements - RunTime for Queue - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * REMOVE ELEMENT -- Stack vs Queue * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); stack.Pop(); stopWatch.Stop(); Console.WriteLine("\nRemove Elements - RunTime for Stack - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); queue.Dequeue(); stopWatch.Stop(); Console.WriteLine("Remove Elements - RunTime for Queue - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * ADD ELEMENT -- HashTable vs Dictionary * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); for (int i = 0; i < 1000000; i++) { hashTable.Add(i, new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("\nAdd Elements - RunTime for HashTable - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < 1000000; i++) { dictionary.Add(i, new Circle((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements - RunTime for Dictionary - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * SEARCH ELEMENT -- HashTable vs Dictionary * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); result = (Circle)hashTable[500000]; stopWatch.Stop(); Console.WriteLine("\nSearch Elements - RunTime for Hashtable - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); result = (Circle)dictionary[500000]; stopWatch.Stop(); Console.WriteLine("Search Elements - RunTime for Dictionary - " + stopWatch.Elapsed); stopWatch.Reset(); /*----------------------------------------------------------------------------------------- * REMOVE ELEMENT -- HashTable vs Dictionary * ------------------------------------------------------------------------------------------*/ stopWatch.Start(); hashTable.Remove(500000); stopWatch.Stop(); Console.WriteLine("\nRemove Elements - RunTime for HashTable - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); dictionary.Remove(500000); stopWatch.Stop(); Console.WriteLine("Remove Elements - RunTime for Dictionary - " + stopWatch.Elapsed); stopWatch.Reset(); Console.ReadLine(); }
static void Main(string[] args) { #region ArrayList ArrayList arrayKayitlar = new ArrayList(); arrayKayitlar.Add("Ram"); arrayKayitlar.Add("Monitor"); arrayKayitlar.Add("CPU"); arrayKayitlar.Add("Mouse"); arrayKayitlar.Add("Klavye"); arrayKayitlar.Add("Capture"); for (int i = 0; i < arrayKayitlar.Count; i++) { Console.WriteLine(arrayKayitlar[i]); } Console.WriteLine("*******************"); int sonuc = arrayKayitlar.BinarySearch("CPU", new CaseInsensitiveComparer()); if (0 < sonuc) { Console.WriteLine("Kayıt bulundu ve Silinecek :" + arrayKayitlar[sonuc]); } else { Console.WriteLine("Kayıt Bulanamadı..."); } arrayKayitlar.Remove("CPU"); arrayKayitlar.Sort(); Console.WriteLine("********Sıralandı*******"); for (int i = 0; i < arrayKayitlar.Count; i++) { Console.WriteLine(arrayKayitlar[i]); } #endregion Console.Clear(); #region HashTable Hashtable hashKayitlar = new Hashtable(); hashKayitlar.Add("A", "Ankara"); hashKayitlar.Add("B", "Bursa"); hashKayitlar.Add("D", "Denizli"); if (hashKayitlar.ContainsKey("B")) { Console.WriteLine(hashKayitlar["B"]); } #endregion Console.Clear(); #region SortedList SortedList sortedKayitlar = new SortedList(); sortedKayitlar.Add(3, "Üçüncü"); sortedKayitlar[2] = "ikinci"; sortedKayitlar.Add(4, "Beşinci"); sortedKayitlar[4] = "Dördüncü";//değiştirdik. //for ile dönülebilir indisi vardır for (int i = 0; i < sortedKayitlar.Count; i++) { Console.WriteLine(sortedKayitlar.GetByIndex(i)); } #endregion Console.Clear(); //başka bir thread işlem yapmaz bekler. lock (sortedKayitlar.SyncRoot) { foreach (var item in sortedKayitlar.Values) { Console.WriteLine(item); } } Console.ReadKey(); }
static void Main(string[] args) { //test object Ellipse testEllipse = new Ellipse(4.4, 1.6, 5.3); //ADD ELEMENT -- ArrayList vs LinkedList Stopwatch stopWatch = new Stopwatch(); Random rand = new Random(); ArrayList array = new ArrayList(); stopWatch.Start(); for (int i = 0; i < 500000; i++) { array.Add(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } array.Add(testEllipse); for (int i = 0; i < 500000; i++) { array.Add(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements: TestTime for ArrayList - " + stopWatch.Elapsed); stopWatch.Reset(); LinkedList <Ellipse> linkedList = new LinkedList <Ellipse>(); stopWatch.Start(); for (int i = 0; i < 500000; i++) { linkedList.AddLast(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } linkedList.AddLast(testEllipse); for (int i = 0; i < 500000; i++) { linkedList.AddLast(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements: TestTime for LinkedList - " + stopWatch.Elapsed); stopWatch.Reset(); Ellipse result = new Ellipse(); //SEARCH ELEMENT -- ArrayList vs LinkedList stopWatch.Start(); array.Sort(new CustomEllipseComparer()); int index = array.BinarySearch((object)testEllipse, new CustomEllipseComparer()); result = (Ellipse)array[index]; stopWatch.Stop(); Console.WriteLine("\nSearch Elements: TestTime for ArrayList - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); result = (Ellipse)linkedList.Find(testEllipse).Value; stopWatch.Stop(); Console.WriteLine("Search Elements: TestTime for LinkedList - " + stopWatch.Elapsed); stopWatch.Reset(); //REMOVE ELEMENT -- ArrayList vs LinkedList stopWatch.Start(); array.Remove(testEllipse); stopWatch.Stop(); Console.WriteLine("\nRemove Elements: TestTime for ArrayList - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); linkedList.Remove(testEllipse); stopWatch.Stop(); Console.WriteLine("Remove Elements: TestTime for LinkedList - " + stopWatch.Elapsed); stopWatch.Reset(); //ADD ELEMENT -- Stack vs Queue Stack <Ellipse> stack = new Stack <Ellipse>(); stopWatch.Start(); for (int i = 0; i < 5000; i++) { stack.Push(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stack.Push(testEllipse); for (int i = 0; i < 5000; i++) { stack.Push(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("\nAdd Elements: TestTime for Stack - " + stopWatch.Elapsed); stopWatch.Reset(); Queue <Ellipse> queue = new Queue <Ellipse>(); stopWatch.Start(); for (int i = 0; i < 5000; i++) { queue.Enqueue(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } queue.Enqueue(testEllipse); for (int i = 0; i < 5000; i++) { queue.Enqueue(new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements: TestTime for Queue - " + stopWatch.Elapsed); stopWatch.Reset(); //SEARCH ELEMENT -- Stack vs Queue CustomEllipseComparer circleComparer = new CustomEllipseComparer(); stopWatch.Start(); for (int i = 0; i < 10000; i++) { if (circleComparer.Compare(testEllipse, stack.ElementAt(i)) == 0) { result = stack.ElementAt(i); } } stopWatch.Stop(); Console.WriteLine("\nSearch Elements: TestTime for Stack - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < 10000; i++) { if (circleComparer.Compare(testEllipse, queue.ElementAt(i)) == 0) { result = queue.ElementAt(i); } } stopWatch.Stop(); Console.WriteLine("Search Elements: TestTime for Queue - " + stopWatch.Elapsed); stopWatch.Reset(); //REMOVE ELEMENT -- Stack vs Queue stopWatch.Start(); stack.Pop(); stopWatch.Stop(); Console.WriteLine("\nRemove Elements: TestTime for Stack - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); queue.Dequeue(); stopWatch.Stop(); Console.WriteLine("Remove Elements: TestTime for Queue - " + stopWatch.Elapsed); stopWatch.Reset(); //ADD ELEMENT -- HashTable vs Dictionary Dictionary <int, Ellipse> dictionary = new Dictionary <int, Ellipse>(); Hashtable hashTable = new Hashtable(); stopWatch.Start(); for (int i = 0; i < 1000000; i++) { hashTable.Add(i, new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("\nAdd Elements: TestTime for HashTable - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); for (int i = 0; i < 1000000; i++) { dictionary.Add(i, new Ellipse((rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1), (rand.Next(20) + rand.Next(20) * 0.1))); } stopWatch.Stop(); Console.WriteLine("Add Elements: TestTime for Dictionary - " + stopWatch.Elapsed); stopWatch.Reset(); //SEARCH ELEMENT -- HashTable vs Dictionary stopWatch.Start(); result = (Ellipse)hashTable[500000]; stopWatch.Stop(); Console.WriteLine("\nSearch Elements: TestTime for Hashtable - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); result = (Ellipse)dictionary[500000]; stopWatch.Stop(); Console.WriteLine("Search Elements: TestTime for Dictionary - " + stopWatch.Elapsed); stopWatch.Reset(); //REMOVE ELEMENT -- HashTable vs Dictionary stopWatch.Start(); hashTable.Remove(500000); stopWatch.Stop(); Console.WriteLine("\nRemove Elements: TestTime for HashTable - " + stopWatch.Elapsed); stopWatch.Reset(); stopWatch.Start(); dictionary.Remove(500000); stopWatch.Stop(); Console.WriteLine("Remove Elements: TestTime for Dictionary - " + stopWatch.Elapsed); stopWatch.Reset(); Console.WriteLine("\nAll operations have ended. Press enter to exit"); Console.ReadLine(); }