﻿ Lesson 33: Introduction to Arrays and Lists
 Introduction to Arrays and Lists

 A List as an Array of Values

 Introduction

A database is a list of values. The values can be organized to make it easy to retrieve and optionally manipulate them. A computer database is a list of values that are stored in the computer, usually as one or more files. The values can then be accessed when needed. Probably the most fundamental type of list of values can be created, and managed, as an array.

 Creating an Array

Before creating an array, you must decide what type of values each element of the array would be. A simple array can be made of values of primitive types. Here is an example:

```void btnWriteClick(object sender, EventArgs e)
{
var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };
}```

Once the array has been created, you can access each one of its elements using the [] operator. Here is an example:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvValues;

public Exercise()
{
InitializeComponent();
}

void InitializeComponent()
{

dgvValues = new DataGridView();
dgvValues.Location = new Point(12, 40);

StartPosition = FormStartPosition.CenterScreen;
}

{
var numbers = new double[] { 12.44, 525.38, 6.28, 2448.32, 632.04 };

for (int i = 0; i < 5; i++)
{
dgvValues.Rows[i].Cells[0].Value = numbers[i].ToString();
}
}
}

public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}```

An array can also be made of elements that are each a composite type. That is, each element can be of a class type. Of course, you must have a class first. You can use one of the many built-in classes of the .NET Framework or you can create your own class. After creating the class, you can then use it as the type of the array. Here is an example:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvValues;

public Exercise()
{
InitializeComponent();
}

void InitializeComponent()
{

dgvValues = new DataGridView();
dgvValues.Location = new Point(12, 44);
dgvValues.Size = new Size(270, 200);
dgvValues.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;

Text = "Students Records";
Size = new Size(320, 280);
StartPosition = FormStartPosition.CenterScreen;
}

{
var people = new Student[]
{
new Student()
{
StudentID = 72947, Gender = Genders.Female,
FirstName = "Paulette", LastName = "Cranston"
},

new Student()
{
StudentID = 70854, Gender = Genders.Male,
FirstName = "Harry", LastName = "Kumar"
},

new Student()
{
StudentID = 27947, Gender = Genders.Male,
FirstName = "Jules", LastName = "Davidson"
},

new Student()
{
StudentID = 62835, Gender = Genders.Unknown,
FirstName = "Leslie", LastName = "Harrington"
},

new Student()
{
StudentID = 92958, Gender = Genders.Male,
FirstName = "Ernest", LastName = "Colson"
},

new Student()
{
StudentID = 91749, Gender = Genders.Female,
FirstName = "Patricia", LastName = "Katts"
},

new Student()
{
StudentID = 29749, Gender = Genders.Unknown,
FirstName = "Patrice", LastName = "Abanda"
},

new Student()
{
StudentID = 24739, Gender = Genders.Male,
FirstName = "Frank", LastName = "Thomasson"
}
};

for (int i = 0; i < 8; i++)
{

dgvValues.Rows[i].Cells[0].Value = people[i].StudentID.ToString();
dgvValues.Rows[i].Cells[1].Value = people[i].FirstName;
dgvValues.Rows[i].Cells[2].Value = people[i].LastName;
dgvValues.Rows[i].Cells[3].Value = people[i].Gender.ToString();
}
}
}

public enum Genders { Male, Female, Unknown };

public class Student
{
public int StudentID;
public string FirstName;
public string LastName;
public Genders Gender;
}

public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}```

 The Array Class

To assist you with creating or managing arrays, the .NET Framework provides the Array class:

```public abstract class Array : ICloneable,
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable```

Every array you create is derived from this class. As a result, all arrays of your program share many characteristics and they get their foundation from the Array class. This which its properties and methods. Once you declare an array variable, it automatically has access to the members of the Array class. For example, instead of counting the number of elements in an array, you can access the Length property of the array variable.

In traditional languages, when you declare an array variable, you must specify the number of elements that the array will contain. You must do this especially if you declare the variable without initializing the array. Here is an example:

` var people = new Person[4];`

After declaring the variable, before using the array, you must initialize it. Otherwise you would receive an error. When initializing the array, you can only initialize it with the number of elements you had specified.

One of the most valuable features of the Array class is that it allows an array to be resized. That is, if you find out that an array has a size smaller than the number of elements you want to add to it, you can increase its capacity. To support this, the Array class is equipped with the static Resize() method. Its syntax is:

`public static void Resize<T>(ref T[] array, int newSize);`

As you can see, this is a generic method that takes two arguments. The first argument is the name of the array variable that you want to resize. It must be passed by reference. The second argument is the new size you want the array to have. Here is an example of calling this method:

```public class Exercise : System.Windows.Forms.Form
{
. . . No Change

{
var people = new Student[4];

people[0] = new Student()
{
StudentID = 72947,
FirstName = "Paulette",
LastName = "Cranston",
Gender = Genders.Female
};

people[1] = new Student()
{
StudentID = 70854,
FirstName = "Harry",
LastName = "Kumar",
Gender = Genders.Male
};

people[2] = new Student()
{
StudentID = 27947,
FirstName = "Jules",
LastName = "Davidson",
Gender = Genders.Male
};

people[3] = new Student()
{
StudentID = 62835,
FirstName = "Leslie",
LastName = "Harrington",
Gender = Genders.Unknown
};

Array.Resize<Student>(ref people, 6);

people[4] = new Student()
{
StudentID = 92958,
FirstName = "Ernest",
LastName = "Colson",
Gender = Genders.Male
};

people[5] = new Student()
{
StudentID = 91749,
FirstName = "Patricia",
LastName = "Katts",
Gender = Genders.Female
};

for (int i = 0; i < people.Length; i++)
{

dgvValues.Rows[i].Cells[0].Value = people[i].StudentID.ToString();
dgvValues.Rows[i].Cells[1].Value = people[i].FirstName;
dgvValues.Rows[i].Cells[2].Value = people[i].LastName;
dgvValues.Rows[i].Cells[3].Value = people[i].Gender.ToString();
}
}
}```
 An Array as a Field

As done previously, you can create an array in a method. A disadvantage of this approach is that the array can be accessed from only the method (or event) in which it is created. As an alternative, you can declare an array as a member of a class. Here is an example:

```public class Exercise : System.Windows.Forms.Form
{
Student[] Students;
}```

The advantage of this approach is that you can access the array from any member of the same class or even from another file of the same program. After declaring the variable, you can initialize it. You can do this in a constructor or in an event that would be fired before any other event that would use the array. This type of initialization is usually done in a Load event of a form. After initializing the array, you can then access it in another method or another event of the form. Here is an example:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvValues;
Student[] Students;

public Exercise()
{
InitializeComponent();
}

void InitializeComponent()
{

dgvValues = new DataGridView();
dgvValues.Location = new Point(12, 44);
dgvValues.Size = new Size(270, 200);
dgvValues.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;

Text = "Students Records";
Size = new Size(320, 280);
StartPosition = FormStartPosition.CenterScreen;
}

{
Students = new Student[4];

Students[0] = new Student()
{
StudentID = 72947,
FirstName = "Paulette",
LastName = "Cranston",
Gender = Genders.Female
};

Students[1] = new Student()
{
StudentID = 70854,
FirstName = "Harry",
LastName = "Kumar",
Gender = Genders.Male
};

Students[2] = new Student()
{
StudentID = 27947,
FirstName = "Jules",
LastName = "Davidson",
Gender = Genders.Male
};

Students[3] = new Student()
{
StudentID = 62835,
FirstName = "Leslie",
LastName = "Harrington",
Gender = Genders.Unknown
};

Array.Resize<Student>(ref Students, 6);

Students[4] = new Student()
{
StudentID = 92958,
FirstName = "Ernest",
LastName = "Colson",
Gender = Genders.Male
};

Students[5] = new Student()
{
StudentID = 91749,
FirstName = "Patricia",
LastName = "Katts",
Gender = Genders.Female
};

}

{

for (int i = 0; i < Students.Length; i++)
{

dgvValues.Rows[i].Cells[0].Value = Students[i].StudentID.ToString();
dgvValues.Rows[i].Cells[1].Value = Students[i].FirstName;
dgvValues.Rows[i].Cells[2].Value = Students[i].LastName;
dgvValues.Rows[i].Cells[3].Value = Students[i].Gender.ToString();
}
}
}

public enum Genders { Male, Female, Unknown };

public class Student
{
public int StudentID;
public string FirstName;
public string LastName;
public Genders Gender;
}

public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}```
 Arrays and Methods

An array can be passed as argument to a method. Here is an example:

```public class Exercise : System.Windows.Forms.Form
{
void ArrayInitializer(Student[] People)
{
}
}```

In the method, you can use the array as you see fit. For example you can assign values to the elements of the array. When calling a method that is passed an array, you can pass the argument by reference so it would come back with new values. Here are examples:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvValues;

public Exercise()
{
InitializeComponent();
}

void InitializeComponent()
{

dgvValues = new DataGridView();
dgvValues.Location = new Point(12, 44);
dgvValues.Size = new Size(270, 200);
dgvValues.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;

Text = "Students Records";
Size = new Size(320, 280);
StartPosition = FormStartPosition.CenterScreen;
}

private void InitializeStudents(ref Student[] People)
{
People = new Student[]
{
new Student()
{
StudentID = 72947, FirstName = "Paulette",
LastName = "Cranston", Gender = Genders.Female
},

new Student()
{
StudentID = 70854, FirstName = "Harry",
LastName = "Kumar", Gender = Genders.Male
},

new Student()
{
StudentID = 27947, FirstName = "Jules",
LastName = "Davidson", Gender = Genders.Male
},

new Student()
{
StudentID = 62835, FirstName = "Leslie",
LastName = "Harrington", Gender = Genders.Unknown
},

new Student()
{
StudentID = 92958, FirstName = "Ernest",
LastName = "Colson", Gender = Genders.Male
},

new Student()
{
StudentID = 91749, FirstName = "Patricia",
LastName = "Katts", Gender = Genders.Female
},
};
}

private void ShowStudents(Student[] People)
{
int i = 0;

foreach (Student std in People)
{

dgvValues.Rows[i].Cells[0].Value = std.StudentID.ToString();
dgvValues.Rows[i].Cells[1].Value = std.FirstName;
dgvValues.Rows[i].Cells[2].Value = std.LastName;
dgvValues.Rows[i].Cells[3].Value = std.Gender.ToString();

i++;
}
}

{
}

{
Student[] Students = new Student[8];

InitializeStudents(ref Students);
ShowStudents(Students);
}
}

public enum Genders { Male, Female, Unknown };

public class Student
{
public int StudentID;
public string FirstName;
public string LastName;
public Genders Gender;
}

public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}```

A method can be created to return an array. When creating the method, on the left side of the name of the method, type the name of the type of value the method would return, including the square brackets. In the method, create and initialize an array. Before exiting the method, you must return the array. Here is an example:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
. . . No Change

private Student[] InitializeStudents()
{
Student[] People = new Student[]
{
new Student()
{
StudentID = 72947, FirstName = "Paulette",
LastName = "Cranston", Gender = Genders.Female
},

new Student()
{
StudentID = 70854, FirstName = "Harry",
LastName = "Kumar", Gender = Genders.Male
},

new Student()
{
StudentID = 27947, FirstName = "Jules",
LastName = "Davidson", Gender = Genders.Male
},

new Student()
{
StudentID = 62835, FirstName = "Leslie",
LastName = "Harrington", Gender = Genders.Unknown
},

new Student()
{
StudentID = 92958, FirstName = "Ernest",
LastName = "Colson", Gender = Genders.Male
},

new Student()
{
StudentID = 91749, FirstName = "Patricia",
LastName = "Katts", Gender = Genders.Female
},
};

return People;
}

private void ShowStudents(Student[] People)
{
int i = 0;

foreach (Student std in People)
{

dgvValues.Rows[i].Cells[0].Value = std.StudentID.ToString();
dgvValues.Rows[i].Cells[1].Value = std.FirstName;
dgvValues.Rows[i].Cells[2].Value = std.LastName;
dgvValues.Rows[i].Cells[3].Value = std.Gender.ToString();

i++;
}
}

{
}

{
Student[] Students = InitializeStudents();
ShowStudents(Students);
}
}```
 Operations on an Array

 Introduction

Because an array is primarily a series of objects or values, there are various pieces of information you would get interested to get from it. Typical operations include:

• Adding elements to the array
• Re-arranging the list or the order of elements in the array.
• Finding out whether the array contains a certain element
• If the array contains a certain element, what index that element has

Although you can write your own routines to perform these operations, the Array class provides most of the methods you would need to get the necessary information.

 Adding an Element to an Array

We have seen that, using the [] operator, you can add a new element to an array. Still, to support this operation, the Array class is equipped with the SetValue() method that is overloaded with various versions. Here is an example that adds an element to the third position of the array:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
DataGridView dgvValues;
Student[] Students;

public Exercise()
{
InitializeComponent();
}

void InitializeComponent()
{

dgvValues = new DataGridView();
dgvValues.Location = new Point(12, 44);
dgvValues.Size = new Size(270, 210);
dgvValues.Anchor = AnchorStyles.Left | AnchorStyles.Top |
AnchorStyles.Right | AnchorStyles.Bottom;

Text = "Students Records";

Size = new Size(320, 280);
StartPosition = FormStartPosition.CenterScreen;
}

{
Students = new Student[4];

Students[0] = new Student()
{
StudentID = 72947,
FirstName = "Paulette",
LastName = "Cranston",
Gender = Genders.Female
};

Students[1] = new Student()
{
StudentID = 70854,
FirstName = "Harry",
LastName = "Kumar",
Gender = Genders.Male
};

Students[2] = new Student()
{
StudentID = 27947,
FirstName = "Jules",
LastName = "Davidson",
Gender = Genders.Male
};

Students[3] = new Student()
{
StudentID = 62835,
FirstName = "Leslie",
LastName = "Harrington",
Gender = Genders.Unknown
};

Array.Resize<Student>(ref Students, 6);

Students[4] = new Student()
{
StudentID = 92958,
FirstName = "Ernest",
LastName = "Colson",
Gender = Genders.Male
};

Students[5] = new Student()
{
StudentID = 91749,
FirstName = "Patricia",
LastName = "Katts",
Gender = Genders.Female
};

ShowStudents(Students);
}

private void ShowStudents(Student[] People)
{
int i = 0;

dgvValues.Rows.Clear();

foreach (Student std in People)
{

dgvValues.Rows[i].Cells[0].Value = std.StudentID.ToString();
dgvValues.Rows[i].Cells[1].Value = std.FirstName;
dgvValues.Rows[i].Cells[2].Value = std.LastName;
dgvValues.Rows[i].Cells[3].Value = std.Gender.ToString();

i++;
}
}

{
var std = new Student();

std.StudentID = 511824;
std.FirstName = "Hermine";
std.LastName  = "Noland";
std.Gender    = Genders.Female;

Students.SetValue(std, 3);
ShowStudents(Students);
}
}

public enum Genders { Male, Female, Unknown };

public class Student
{
public int StudentID;
public string FirstName;
public string LastName;
public Genders Gender;
}

public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}```

When the Array.SetValue() method is called, it replaces the element at the indicated position.

 Getting an Element From an Array

The reverse of adding an item to an array is to retrieve one. You can do this with the [] operator. To support this operation, the Array class is equipped with the GetValue() method that comes in various versions. Here is an example of calling it:

`Student pers = (Student)People.GetValue(2);`

When calling this method, make sure you provide a valid index, if you don't, you would get an IndexOutOfRangeException exception.

 Checking the Existence of an Element

Once some elements have been added to an array, you can try to locate one. One of the most fundamental means of looking for an item consists of finding out whether a certain element exists in the array. To support this operation, the Array class is equipped with the Exists() method whose syntax is:

`public static bool Exists<T>(T[] array, Predicate<T> match);`

This is a generic method that takes two arguments. The first is the array on which you will look for the item. The second argument is a delegate that specifies the criterion to apply. Here is an example:

```using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
. . . No Change

static int IDToFind;

public Exercise()
{
InitializeComponent();
}

void InitializeComponent()
{
. . . No Change
}

{
. . . No Change
}

private void ShowStudents(Student[] People)
{
. . . No Change
}

{
. . . No Change
}

static bool IDExists(Student std)
{
if( std.StudentID == Exercise.IDToFind)
return true;
else
return false;
}

private void btnExists_Click(object sender, EventArgs e)
{
IDToFind = int.Parse(txtStudentID.Text);

if (Array.Exists<Student>(Students, IDExists))
MessageBox.Show("The student was found");
else
}
}

public enum Genders { Male, Female, Unknown };

public class Student
{
public int StudentID;
public string FirstName;
public string LastName;
public Genders Gender;
}

public class Program
{
static int Main()
{
System.Windows.Forms.Application.Run(new Exercise());
return 0;
}
}```

 Finding an Element

One of the most valuable operations you can perform on an array consists of looking for a particular element. To assist you with this, the Array class is equipped with the Find() method. Its syntax is:

`public static T Find<T>(T[] array, Predicate<T> match);`

This generic method takes two arguments. The first argument is the name of the array that holds the elements. The second argument is a delegate that has the criterion to be used to find the element. Here is an example:

```private void btnFind_Click(object sender, EventArgs e)
{
IDToFind = int.Parse(txtPersonID.Text);

Student pers = Array.Find<Student>(Students, IDExists);

if (pers != null)
{
txtFirstName.Text = pers.FirstName;
txtLastName.Text = pers.LastName;
cbxGenders.Text = pers.Gender.ToString();
}
}```