public void ArrayListOperations() { ArrayList arrayList = new ArrayList(); arrayList.Add(10); arrayList.Add(89); arrayList.Add(29); arrayList.Add(-1); arrayList.Add(15); PrintArrayList(arrayList); arrayList.Sort(); arrayList.Insert(2, 18); PrintArrayList(arrayList); Console.WriteLine("\nArraylist capacity\t" + arrayList.Capacity); int hasElement = arrayList.IndexOf(23); if (hasElement >= 0) { Console.WriteLine("Element is present"); } else { Console.WriteLine("Element is not present"); } arrayList.TrimToSize(); Console.WriteLine("\nArraylist capacity\t" + arrayList.Capacity); int[] arr = { 14, 17 }; arrayList.AddRange(arr); PrintArrayList(arrayList); arrayList.InsertRange(3, arr); PrintArrayList(arrayList); arrayList.SetRange(7, arr); PrintArrayList(arrayList); }
static public void ArrayLists1() { var arlist1 = new ArrayList { 1, ' ', " Mustafa ", 3.14M, true, 4.5, null }; var myQueue = new Queue(); myQueue.Enqueue("Mustafa"); myQueue.Enqueue(20); arlist1.AddRange(myQueue); arlist1.Insert(1, "Second Item"); ArrayList arlist2 = new ArrayList() { 300, 400, 500 }; arlist1.InsertRange(3, arlist2); Console.WriteLine("-*-*-*-*-* Before Remove *-*-*-*-*-"); for (var i = 0; i <= arlist1.Count - 1; i++) { Console.WriteLine(arlist1[i]); } Console.WriteLine("-*-*-*-*-* After Remove *-*-*-*-*-"); arlist1.Remove(null); //Removes first occurance of null arlist1.RemoveAt(4); //Removes element at index 4 arlist1.RemoveRange(0, 2); //Removes two elements starting from 1st item (0 index) foreach (var item in arlist1) { Console.WriteLine(item); } Console.WriteLine(arlist1.Contains(300)); // true Console.WriteLine(arlist1.Contains("Mustafa")); // true Console.WriteLine(arlist1.Contains(10)); // false Console.WriteLine(arlist1.Contains("Bazı Bazı")); // false arlist1.Clear(); }
static void Main(string[] args) { //Arraylist ArrayList myList = new ArrayList(); myList.Add(1); myList.Add("Hi"); ArrayList myList2 = new ArrayList(); myList2.Add("Hello"); myList.AddRange(myList2); myList.Insert(1, 4); ArrayList myList3 = new ArrayList() { 10, 20, 30 }; myList.InsertRange(3, myList3); for (int i = 0; i < myList.Count; i++) { Console.WriteLine(myList[i]); } Console.WriteLine(myList.Contains(10)); }
static void Main(string[] args) { //ArrayList ArrayList al = new ArrayList(); int a = 101; al.Add(a); al.Add("This is a arraylist"); al.Add(2.3f); al.Insert(3, "This is the forth index"); ArrayList al2 = new ArrayList(); al2.Add(102); al.InsertRange(1, al2); Console.WriteLine("ArrayList:"); Console.WriteLine(); foreach (var ele in al) { Console.WriteLine(ele + " "); } Console.WriteLine("The count of the arraylist is {0}", al.Count); Console.WriteLine("The capacity of the arraylist is {0}", al.Capacity); Console.WriteLine(); // Hash table Hashtable ht = new Hashtable(); ht.Add(1, "one"); ht.Add(2, "two"); ht.Add(0.3f, 2); ht.Add(4, null); ht.Add("Hema", "vathi"); ht.Remove(2); // ht.Clear(); Console.WriteLine("Hashtable:"); Console.WriteLine(); foreach (DictionaryEntry ele in ht) //ht.keys means only print a key values for values the viceversa is applied. { Console.WriteLine("{0},{1}", ele.Key, ele.Value); } String firstelement = (string)ht[1]; String str = (string)ht["Hema"]; Console.WriteLine("The first element in the hashtable is {0}", firstelement); Console.WriteLine("The last element in the hashtable is {0}", str); Console.WriteLine("The hash table contains a 0.3 is {0}", ht.Contains(0.3f)); Console.WriteLine(); //SortedList Console.WriteLine("Sorted List:"); SortedList sl = new SortedList(); sl.Add(2, "two"); sl.Add(1, "one"); sl.Add(0, "zero"); sl.RemoveAt(0); foreach (DictionaryEntry i in sl) { Console.WriteLine("{0},{1}", i.Key, i.Value); } Console.WriteLine(); //stack Console.WriteLine("Stack:"); Stack s = new Stack(); s.Push("hello"); s.Push(102); s.Push(102); s.Push(.3f); foreach (var item in s) { Console.WriteLine(item); } Console.WriteLine("The first element will be deleted is {0} ", s.Peek()); Console.WriteLine(); //Queue Console.WriteLine("Queue:"); Queue q = new Queue(); q.Enqueue(34); q.Enqueue("thirtyfour"); q.Enqueue(3.4); q.Dequeue(); foreach (var item in q) { Console.WriteLine(item); } Console.WriteLine("The first element of the queue is {0}", q.Peek()); }