0% found this document useful (0 votes)
81 views

ArrayList and MethodsExample Program in C#

The document discusses implementing an array list in C# by developing a program with properties and methods for an array list. It includes source code that defines an ArrayList class with methods to add, insert, remove, sort, search, and display elements in the array list. The main method initializes an ArrayList and uses a switch statement to call the various methods based on user input.

Uploaded by

Mohan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

ArrayList and MethodsExample Program in C#

The document discusses implementing an array list in C# by developing a program with properties and methods for an array list. It includes source code that defines an ArrayList class with methods to add, insert, remove, sort, search, and display elements in the array list. The main method initializes an ArrayList and uses a switch statement to call the various methods based on user input.

Uploaded by

Mohan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1-ARRAY LIST

AIM:
Using C# Programming, develop the program, which implements the properties and
methods in Array List.

SOURCE CODE:
using System;
using System.Collections;

namespace ArrayClass
{
class Arraylisteg
{
static void display(ArrayList array)
{
Console.WriteLine("Length of the Array List are {0}", array.Count);
Console.WriteLine("Elements in an Array are ...");
foreach(var elements in array)
{
Console.WriteLine(elements);

}
}
static void addIndividual(ArrayList array)
{
Console.Write("Enter the Element to Add :");
array.Add(Console.ReadLine());
Console.Write("Element Added Successfully");
}

static void addRange(ArrayList array)


{
Console.Write("Enter the n value :");
int n = Convert.ToInt32(Console.ReadLine());

Array ar = Array.CreateInstance(typeof(string), n);

for (int i = 0; i < n; i++)


{
Console.Write("Enter the Values:");
ar.SetValue(Console.ReadLine(), i);
}

array.AddRange(ar);
Console.WriteLine(" Values Inserted in Array List Successfully");
}
static void insertElement(ArrayList array)
{
Console.Write("Enter the location to Insert:");
int n = Convert.ToInt32(Console.ReadLine());
if (n > array.Count)
{
Console.WriteLine("Not Sufficient Space to Insert :");

1|Page
}
else
{
Console.Write("Enter the Value to Insert :");
string value = Console.ReadLine();
array.Insert(n, value);
Console.WriteLine(" Values Inserted in Array List Successfully");
}
}
static void insertRange(ArrayList array)
{
Console.Write("Enter the number of items to insert :");
int n = Convert.ToInt32(Console.ReadLine());
Array ar = Array.CreateInstance(typeof(string), n);

Console.Write("Enter the Location to Insert:");


int loc = Convert.ToInt16(Console.ReadLine());
if (loc >= array.Count)
{
Console.WriteLine("Not Sufficient Space to Insert :");
}
else
{
for (int i = 0; i < n; i++)
{
Console.Write("Enter the Value to Insert :");
ar.SetValue(Console.ReadLine(), i);
}

array.InsertRange(loc, ar);
Console.WriteLine(" Values Inserted in Array List Successfully");
}
}
static void removeIndividual(ArrayList array)
{
string value;
Console.Write("Enter the Element to Remove");
value= Console.ReadLine();

array.Remove(value);
Console.WriteLine("Element Deleted Successfully");
}
static void removeRange(ArrayList array)
{
Console.Write("Enter the starting index :");
int loc = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the no of elements to Delete:");
int n = Convert.ToInt16(Console.ReadLine());
if ((loc+n)>= array.Count)
{
Console.WriteLine("Not Sufficient Space to Delete :");
}
else
{

array.RemoveRange(loc, n);
Console.WriteLine(" Values Deleted in Array List Successfully");
}
}
static void removeLoc(ArrayList array)
{
Console.Write("Enter the Location to Delete");

2|Page
int loc = Convert.ToInt16(Console.ReadLine());

if (loc < array.Count)


{
array.RemoveAt(loc);
Console.WriteLine("Element Deleted Successfully:");
}
else
{
Console.WriteLine("Not Sufficient Space to Delete :");
}
}
static void Search(ArrayList array)
{
Console.Write("Enter the Element to Search :");
string value = Console.ReadLine();

if (array.Contains(value))
{
Console.WriteLine("Element Found Successfully ");
}
else
{
Console.WriteLine("Element Not Found ");
}
}
static void indexvalue(ArrayList array)
{
Console.Write("Enter the value to Search:");
string value = Console.ReadLine();

int count= array.IndexOf(value);


if (count >= 0)
{
Console.WriteLine("Element is found at {0} Location ", count);
}
else
Console.WriteLine("Element is not found:");

}
public static void Main()
{
ArrayList array = new ArrayList(); // Definition and Initialization

int ch;
Array duparray = Array.CreateInstance(typeof(string), 10);
do
{
Console.Clear(); // Clearscreen
Console.WriteLine("1- Add Individual Elements");
Console.WriteLine("2- Add Range of Elements");
Console.WriteLine("3- Insert Elements individually");
Console.WriteLine("4- Insert Range of Elements");
Console.WriteLine("5- Remove Individual Element");
Console.WriteLine("6- Remove Range of ELements");
Console.WriteLine("7- Remove Element at Location");
Console.WriteLine("8- Sort Elements");
Console.WriteLine("9- Reverse the Elements");
Console.WriteLine("10- Search");
Console.WriteLine("11- Clear the list");
Console.WriteLine("12- Duplicate the list");
Console.WriteLine("13- Index of Element");

3|Page
Console.WriteLine("14- Display");
Console.WriteLine("15- Exit");
ch = Convert.ToInt32(Console.ReadLine());

switch (ch)
{
case 1:
addIndividual(array);
Console.ReadKey();
break;
case 2:
addRange(array);
Console.ReadKey();
break;
case 3:
insertElement(array);
Console.ReadKey();
break;
case 4:
insertRange(array);
Console.ReadKey();
break;
case 5:
removeIndividual(array);
Console.ReadKey();
break;
case 6:
removeRange(array);
Console.ReadKey();
break;
case 7:
removeLoc(array);
Console.ReadKey();
break;
case 8:
array.Sort();
Console.WriteLine("Sorted Elements are ...");
display(array);
Console.ReadKey();
break;
case 9:
array.Reverse();
Console.WriteLine("Reversed Array are ..");
display(array);
Console.ReadKey();
break;
case 10:
Search(array);
Console.ReadKey();
break;
case 11:
array.Clear();
Console.WriteLine("Elements of Array are Flushed ");
Console.ReadKey();
break;
case 12:
array.CopyTo(duparray);
Console.Clear();
foreach (string elements in duparray)
{
Console.WriteLine(elements);
}

4|Page
Console.ReadKey();
break;
case 13:
indexvalue(array);
Console.ReadKey();
break;
case 14:
display(array);
Console.ReadKey();
break;
}
}while(ch!=15);
Console.ReadKey();

}
}
}

OUTPUT:

5|Page
6|Page
7|Page
8|Page
9|Page
10 | P a g e
11 | P a g e
12 | P a g e
13 | P a g e
14 | P a g e
15 | P a g e
16 | P a g e
17 | P a g e
RESULT:
Thus, Array list and its methods are implemented successfully.

18 | P a g e

You might also like