The following code shows you working with arraylist.
// Declaring An ArrayList
ArrayList aryList = new ArrayList();
// Adding objects in arrayList
aryList.Add("Osama");
aryList.Add("Mursleen");
aryList.Add("Maverick");
aryList.Add("Veracious");
aryList.Add("Mascot");
// Displaying arrayList elements
foreach (string o in aryList)
Console.WriteLine((o));
// Displaying itmes count in arraylist (Displays 5 items)
Console.WriteLine("The ArrayList Has " + aryList.Count + " Items");
// In this way we can also add elements in arrayList
aryList.AddRange(new string[] {"Hello", "world", "this", "is", "a", "test"});
// Displaying itmes count in arraylist (Now , Displays 11 items)
Console.WriteLine("The ArrayList Has " + aryList.Count + " Items");
// Reverse arrayList elements
aryList.Reverse();
// Searching an object in arrayList and returns its location
Console.WriteLine(aryList.BinarySearch("this"));
// Checking whether specified object is in arrayList or not?
if (aryList.Contains("a"))
{
Console.WriteLine("Element In ArrayList");
}
// Removes the first occurence of the specified object
aryList.Remove("test");
// Removes the element at specified index starting from 0th index
aryList.RemoveAt(3);
// Removes a range of elements from 1 to 3
aryList.RemoveRange(1, 3);
// Clear the arrayList
aryList.Clear();
0 comments:
Post a Comment