57.ArrayList in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace arraylist
{
class Program
{
static void Main(string[] args)
{
ArrayList arr1 = new ArrayList();
arr1.Add("Milan");
arr1.Add("Krunal");
arr1.Add("Divya");
arr1.Add("Riya");
arr1.Add("Divi");
foreach (String val in arr1)
{
Console.WriteLine(val);
}
Stack s1 = new Stack();
s1.Push(5);
s1.Push(15);
s1.Push(25);
s1.Push(35);
s1.Push(45);
Console.WriteLine("==== After Add StackObject into arraylist ====");
arr1.AddRange(s1);
foreach(object val in arr1)
{
Console.WriteLine(val);
}
ArrayList arr2 = new ArrayList();
arr2.Add("Milan2");
arr2.Add("Krunal2");
arr2.Add("Divya2");
arr2.Add("Riya2");
arr2.Add("Divi2");
int pos = arr2.BinarySearch("Divi2");
// Console.WriteLine("======= After Search Element Position is =========");
Console.WriteLine("Position is : " + pos.ToString());
arr2.Insert(3, "Devanshi");
Console.WriteLine("====== AFter Insert Method ======== ");
foreach (object val in arr2)
{
Console.WriteLine(val);
}
arr2.InsertRange(3, s1);
Console.WriteLine("======== After Insert Range Method ===== ");
foreach (object val in arr2)
{
Console.WriteLine(val);
}
arr2.Remove("Divi2");
Console.WriteLine("====== AFter Remove Method ======== ");
foreach (object val in arr2)
{
Console.WriteLine(val);
}
arr2.RemoveAt(2);
Console.WriteLine("====== AFter Remove At Method ======== ");
foreach (object val in arr2)
{
Console.WriteLine(val);
}
arr2.RemoveRange(2, 4);
Console.WriteLine("======== AFter Remove Range Method =====");
foreach (object val in arr2)
{
Console.WriteLine(val);
}
Console.WriteLine("Contains() Result is :" + arr2.Contains("ABC"));
Console.WriteLine("Size of Array List is :" + arr2.Capacity);
arr2.Capacity = 50;
Console.WriteLine("Now Size of Array List is :" + arr2.Capacity);
arr2.TrimToSize();
Console.WriteLine("After Trim the Size Of Array List is :" + arr2.Capacity);
Console.WriteLine("Total Element in ArrayList is :" + arr2.Count);
Console.Read();
}
}
}
Comments
Post a Comment