C Sharp (C#) : Benadir University
C Sharp (C#) : Benadir University
Course: C#
Class: Batch13
Chapter Seven
DataType[] arrayName;
• Or simply,
int[] numbersArray = new int[] { 10, 20, 30, 40, 50 };
• And even,
int[] numbersArray = { 10, 20, 30, 40, 50 };
int index = 0;
StreamReader inputFile;
inputFile = File.OpenText(“Values.txt”);
Random rn = new Random(); for (int row = 0; row < ROWS; row++)
{
for (int row = 0; row < ROWS; row++) for (int col = 0; col < COLS; col++)
{ {
for (int col = 0; col < COLS; col++) listBox.Items.Add(score[row, col].ToString());
{ }
score[row, col] = rn.Next(100); }
}
}
7.7 Jagged Arrays
• A jagged array is special type of 2D array
– rows in a jagged array can have different lengths
int[][] jagArr = new int[3][];
jagArr[0] = new int[4] { 1, 2, 3, 4 } // 4 columns
jagArr[1] = new int[3] { 5, 6, 7 } // 3 columns
jagArr[2] = new int[5] { 8, 9, 10, 11, 12 } // 5 columns
– For example,
List<string> names = new List<string>(); // a List that holds strings
List<int> numbers = new List<int>(); // a List that holds integers
Add or Remove Items
• To add items, use the Add method
List<string> nameList = new List<string>();
nameList.Add(“Chris”);
nameList.Add(“Bill”);
nameList.Remove(“Bill”);
nameList.RemoveAt(0);
Initializing a List Implicitly
• To initialize a List implicitly, simply defines its items when
you declare it
List<int> numberList = new List<int>() { 1, 2, 3 };
List<string>nameList = new List<string>() { “Christ”, “Kathryn”, “Bill” }
Questions?