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

14.Array&ArrayList

The document provides an overview of a course on Arrays and ArrayLists, detailing their objectives, content, and usage in programming. It explains how to create and manipulate arrays and ArrayLists in C#, including examples for storing student marks and employee details. Additionally, it covers operations such as adding, editing, and deleting elements in an ArrayList with practical code samples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

14.Array&ArrayList

The document provides an overview of a course on Arrays and ArrayLists, detailing their objectives, content, and usage in programming. It explains how to create and manipulate arrays and ArrayLists in C#, including examples for storing student marks and employee details. Additionally, it covers operations such as adding, editing, and deleting elements in an ArrayList with practical code samples.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

ILP Content

CONTENTS

COURSE 1 - ARRAY AND ARRAYLIST ........................................................................................................ 1


1.1.1 Objective........................................................................................................................... 1
1.1.2 Course Content................................................................................................................. 1
1.1.3 Array................................................................................................................................. 1
1.1.4 Array list............................................................................................................................ 4

Confidential
ILP Content

COURSE 1 - ARRAY AND ARRAYLIST


1.1.1 Objective
• To store and process with a set of objects.
• Learn Array, Arraylist and its usage.

1.1.2 Course Content

1.1.3 Array

• Array is used to store multiple values of same type in a variable.


• It is used to store values of value types and reference types
• Size of the array should be decided at the time of creation itself.

Syntax:

dataType[] varName=new dataType[size];

Eg int[] marks=new int[10];

▪ marks is an integer array which can hold 10 integer values.


▪ Array store from index 0 till size-1 values. 'marks' array can hold
values from marks[0] to marks[9].

Sample which stores and retrieve the marks of 5 subjects


static void Main(string[] args)
{
int[] marks = new int[5];
Console.WriteLine("Enter marks of 5 subjects");
for (int i = 0; i < 5; i++)
{
marks[i] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine("Marks are as follows: ");
for (int i = 0; i < 5; i++)
{
Console.WriteLine(marks[i]);
}
Console.ReadKey();
}
Output:
Enter marks of 5 subjects
78
89
Confidential 1
ILP Content

67
98
72
Marks are as follows:
78
89
67
98
72

In case we have to store set of employee details then we can use array of type
class. So in the following sample, for each employee we have to store name, id
and performance pay. So first we create an employee array with the required
attributes and a constructor is written to assign the values and there are
properties used to retrieve the values.

In our main program the employee array is created and values are stored and
finally retrieved. The size of employee array is 100. So it can store a maximum of
100 employees.
//In the following line and Array of type employee is created
Employee[] empList = new Employee[100];
//In the following line the object of type employee is created by passing the
required values and storing it in array.
empList[i] = new Employee(name, id, performancePay);
//In the following line the Id of employee is accessed from array.
Console.WriteLine("Id is "+empList[i].Id);

Sample:
class Employee
{
string name;
int id;
int performancePay;

public Employee(string name, int id, int performancePay)


{
this.name = name;
this.id = id;
this.performancePay = performancePay;
}

public string Name


{
get
{
return (name);
}
set
{
name = value.ToUpper();
}
Confidential 2
ILP Content

}
public int PerformancePay
{
get
{
return (performancePay);
}
set
{
performancePay = value;
}
}
public int Id
{
get
{
return (id);
}
set
{
id = value;
}
}
class Program
{
static void Main(string[] args)
{
//Array of type employee is created
Employee[] empList = new Employee[100];
string name;
int id;
int performancePay;
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Enter name,id,performance pay");
name=Console.ReadLine();
id=Convert.ToInt32(Console.ReadLine());
performancePay=Convert.ToInt32(Console.ReadLine());
//ith employee object is created
empList[i] = new Employee(name, id, performancePay);

}
//View the details of all the employees
for (int i = 0; i < 100; i++)
{
Console.WriteLine("Details of Employee no :"+ i+1);
Console.WriteLine("Id is "+empList[i].Id);
Console.WriteLine("Name is "+empList[i].Name);
Console.WriteLine("Performance Pay is
"+empList[i].PerformancePay);

}
Console.ReadKey();
}
}

Output:

Confidential 3
ILP Content

1.1.4 Array list


• Array list provides a dynamically sized array of objects
• There is no size limit for array list
• It is a non-generic collection. We can store values of any type
• Appending elements is efficient compared to Array, but inserting elements
can be slow.
• We can perform add, edit, delete and display in array list.
• In the top of the page along with other using statements add

using System.Collections; to access array list class.


Syntax for array list declaration
ArrayList list = new ArrayList();
Syntax to add elements:
listName.Add(object);
object can be of any type.
Syntax to remove an element from list:
listName.Remove(object);

It is simple for Array list to store 3 values, display them and remove a value

static void Main(string[] args)


{
ArrayList list = new ArrayList();
//Adds value to the list
list.Add(10);
list.Add(20);
list.Add(30);
//Display all the values in a list
foreach (int i in list)
{
Console.WriteLine(i);
}
//Remove the values from the list
list.Remove(20);
//Display all the values in a list
foreach (int i in list)
{
Console.WriteLine(i);
}
Console.ReadKey();
}

Output
10 ----All the values in the list
20
30
Confidential 4
ILP Content

10---The result after removing 20 from the list


30

A company has a set of departments. We will see how to add department to the
arraylist, how to change the existing details, how to delete a department and
finally view the department's details
class Department
{
string name;
int id;
public Department(string name, int id)
{
this.name = name;
this.id = id;
}
public string Name
{
get
{
return (name);
}
set
{
name = value;
}
}

public int Id
{
get
{
return (id);
}
set
{
id = value;
}
}
}

class Program
{
static void Main(string[] args)
{
ArrayList list = new ArrayList();
int choice;
string name;
int id;
do
{
Console.WriteLine("Menu");
Console.WriteLine("1.Add new department");
Console.WriteLine("2.Change department name");
Console.WriteLine("3.Delete a department");
Console.WriteLine("4.View all departments");
Console.WriteLine("5.Exit");
Confidential 5
ILP Content

Console.WriteLine("Enter your choice");


choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Enter id,name");
id = Convert.ToInt32(Console.ReadLine());
name = Console.ReadLine();
//Create a department object
Department d = new Department(name, id);
//Add the department object to the list
list.Add(d);
break;
case 2:
Console.WriteLine("Enter id of the department");
id = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the new name of the department");
name = Console.ReadLine();
//Loop through the department array list to find if the
department exists and change the name if the department with the specified id
exists
foreach (Department dept in list)
{
//checks the department id
if (dept.Id == id)
{
//change the name of the department
dept.Name = name;
}
}
break;
case 3:
Console.WriteLine("Enter id of the department");
id = Convert.ToInt32(Console.ReadLine());
//Loop through the department array list to find if the
department with that id exists and delete the department
foreach (Department dept in list)
{
//checks the department id
if (dept.Id == id)
{
//removes the department from the list
list.Remove(dept);
//break statement is mandatory since an item is
removed from the list the iteration can not continue further
break;
}
}
break;
case 4:
//Loop through the department list and view all the
department details
foreach (Department dept in list)
{
Console.WriteLine("Id is " + dept.Id);
Console.WriteLine("Name is " + dept.Name);
}

Confidential 6
ILP Content

break;
}
} while (choice != 5);

Console.ReadKey();
}
}

Output
Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
1
Enter id,name
1
Admin

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
1
Enter id,name
2
HR

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
4
Id is 1
Name is Admin
Id is 2
Name is HR

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
Confidential 7
ILP Content

2
Enter id of the department
2
Enter the new name of the department
IS

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
4
Id is 1
Name is Admin
Id is 2
Name is IS

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
3
Enter id of the department
1

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
4
Id is 2
Name is IS

Menu
1.Add new department
2.Change department name
3.Delete a department
4.View all departments
5.Exit
Enter your choice
5

Confidential 8

You might also like