14.Array&ArrayList
14.Array&ArrayList
CONTENTS
Confidential
ILP Content
1.1.3 Array
Syntax:
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 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
It is simple for Array list to store 3 values, display them and remove a value
Output
10 ----All the values in the list
20
30
Confidential 4
ILP Content
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
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