08 Arrays PDF
08 Arrays PDF
Object Oriented
Programming I
Arrays
Öğr. Gör.
İhsan Can İÇİUYAN
2 Object Oriented Programming
Contents
Arrays
Initializing Arrays
Array of Objects
Multidimensional Arrays
System.Array Class
Arrays
Array → a set of contiguous data points of the same type
Arrays
You can fill the array elements index by index:
Initializing Arrays
You can fill the items of an array using array initialization syntax.
You can specify each array item within the curly brackets:
When you initialize an array, you do not need to specify the size of
the array:
string[] stringArray = new string[] { "one", "two", "three" };
The size of the array will be inferred by the number of items within
the curly brackets.
When you initialize an array, the use of the new keyword is optional:
bool[] boolArray = { false, false, true };
6 Object Oriented Programming
Initializing Arrays
If there is a mismatch between the declared size and the number
of initializers, you are issued a compile-time error.
Since there are more initializers than the size of the array, the
compiler will issue an error.
Since there are fewer initializers than the size of the array, the
compiler will issue an error.
7 Object Oriented Programming
You must use the new keyword when using this approach.
// a is really int[]
var a = new[] { 1, 10, 100, 1000 };
// b is really double[]
var b = new[] { 1, 1.5, 2, 2.5 };
// c is really string[]
var c = new[] { "hello", null, "world" };
The items in the array’s initialization list must be of the same type.
Thus, the following generates a compile-time error:
var d = new[] { 1, "one", 2, "two", false };
8 Object Oriented Programming
Array of Objects
System.Object is the ultimate base class to every type.
myObjects[0] = 10;
myObjects[1] = false;
myObjects[2] = new DateTime(2024, 12, 20);
myObjects[3] = "Hello World!";
Multidimensional Arrays
C# supports two varieties of multidimensional arrays:
Rectangular array
Jagged array
10 Object Oriented Programming
Multidimensional Arrays
Rectangular array → an array of multiple dimensions, where each
row is of the same length
// A rectangular array
int[,] myMatrix;
myMatrix = new int[3, 4];
// Populate (3 * 4) array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 4; j++)
{
myMatrix[i, j] = i * j;
}
}
11 Object Oriented Programming
Multidimensional Arrays
Jagged array → contains some number of inner arrays, each of
which may have a different upper limit
// A jagged array
// Here we have an array of 5 different arrays
int[][] myJagArray = new int[5][];
Multidimensional Arrays
You can access the elements of a jagged array as follows:
System.Array Class
Every array you create gathers much of its functionality from the
System.Array class.
System.Array Class
EX: A program that makes use of the static Reverse() and Clear()
methods to print out information about an array of string types.
Code: 57_ArrayClass.cs
Output:
-> Here is the array:
Object Oriented Programming, Data Structures, Introduction to Algorithms,
Index from end operator (^) → specifies that the index is relative
to the end of the sequence
The last item in a sequence is one less than the actual length,
so ^0 would actually cause an error.
20 Object Oriented Programming
To pull out the first two members of the array, create ranges from 0
(the first member) to 2 (one more than the desired index position):
string[] courses = { "OOP", "Data Structures", "Algorithms" };
Index idx1 = 0;
Index idx2 = 2;
Range r = idx1..idx2; // the end of the range is exclusive
foreach (var itm in courses[r])
{
Console.Write(itm + ", ");
}
22 Object Oriented Programming
If the end of the range is left off → the length of the range is used
courses[0..3]
courses[0..^0]
courses[..]
23 Object Oriented Programming
References
Arrays, https://learn.microsoft.com/en-us/dotnet/csharp/language-
reference/builtin-types/arrays