DONG NAI UNIVERSITY OF TECHNOLOGY
DONG NAI UNIVERSITY OF TECHNOLOGY
1
One Dimension Array
2
Multi Dimension Array
3
Array class
4
ArrayList class
5
Dictionary class
DONG NAI UNIVERSITY OF TECHNOLOGY
One Dimension Array
M[0]
5
M[1]
2
M[2]
M[3]
9
7
M[4]
6
- Same name
- Same type
- Get Element by Index ( 0 n-1)
M[5]
0
M[6]
8
DONG NAI UNIVERSITY OF TECHNOLOGY
Declaring & Creating Arrays
•
Programmer specifies the type of the elements of the array
•
new operator to allocate dynamically the number of elements in the array
•
•
Array declarations and initializations need not be in the same statement
In arrays of value types, each element contains one value of the declared type
DONG NAI UNIVERSITY OF TECHNOLOGY
Declaring:
<DataType> [ ] <ArrayName>;
int [ ] M;
Button[ ] arrButton;
Creating:
<ArrayName> =new <DataType>[ ARRAY_SIZE];
M=new int[10];
Button []arrButton=new Button[10];
DONG NAI UNIVERSITY OF TECHNOLOGY
Examples…
int M1[]=new int[10];
for(int i=0;i
M1[i]=i*2;
for(int i=0;i
Console.Write(M1[i] + “”);
foreach(int value in M1)
Console.Write(value + “”);
int M2[]={1,4,2} ;
DONG NAI UNIVERSITY OF TECHNOLOGY
•
•
•
•
Properties & Methods
array_name.Length: return size of array
array_name.GetValue (int index): return an object at position index.
array_name[int index]: return an object at position index.
array_name.SetValue (object value, int index): add or modify an object at position
index
•
array_name[int index] = value: add or modify an object at position index
DONG NAI UNIVERSITY OF TECHNOLOGY
Passing Arrays to Method
•
Pass arrays as arguments to methods by specifying the name of the array
(no brackets)
•
•
Arrays are passed by reference
Individual array elements are passed by value
DONG NAI UNIVERSITY OF TECHNOLOGY
Passing Arrays to Method
static void inputArray(int[] arr)
{
Random ran = new Random();
for (int i = 0; i
arr[i] = ran.Next(100);
}
DONG NAI UNIVERSITY OF TECHNOLOGY
Using out or ref keyword to pass by reference
static void edit(out int n)
{
n = -113;
}
OR:
static void edit(ref int n)
{
n = -113;
}
DONG NAI UNIVERSITY OF TECHNOLOGY
The out parameter must be assigned to before control leaves the current
method
static void edit(out int n)
{
//n = -113;
}
Compile error
DONG NAI UNIVERSITY OF TECHNOLOGY
arr[0]=100;
Use out keyword:
edit(out arr[0]);
Use ref keyword:
edit(ref arr[0]);
Two ways: arr[0] -113
DONG NAI UNIVERSITY OF TECHNOLOGY
7
2
9
0
9
5
4
1
8
0
3
6
Multi Dimension
DONG NAI UNIVERSITY OF TECHNOLOGY
Column 0
Column 1
Column 2
Column 3
M[0, 0]
M[0, 1]
M[0, 2]
M[0, 3]
7
2
9
0
M[1, 0]
M[1, 1]
M[1, 2]
M[1, 3]
9
5
4
1
M[2, 0]
M[2, 1]
M[2, 2]
M[2, 3]
8
0
3
6
Row 0
Row 1
Row 2
Column index (or subscript)
Row index (or subscript)
Array name
DONG NAI UNIVERSITY OF TECHNOLOGY
DataType[ , ] arrName;
arrName = new DataType [rowSize, colSize];
DataType [ , ] arrName =
new DataType [rowSize, colSize];
int[,] M = new int[2, 2];
M[0, 0] = 1; M[0, 1] = 2;
M[1, 0] = 5; M[1, 1] = 3;
int[,] M = { {1,8} , {2,5} };
DONG NAI UNIVERSITY OF TECHNOLOGY
GetLength(int dimension)
int[,] M = {{1,8},{2,5},{1,1} };
row=M.GetLength(0); row=3
col=M.GetLength(1); col=2
int[, ,] M2 = new Int32[3, 4, 5];
x=M2.GetLength(0);x=3
y=M2.GetLength(1);y=4
z=M2.GetLength(2);z=5
DONG NAI UNIVERSITY OF TECHNOLOGY
Examples
for (int i = 0; i < M.GetLength(0); i++)
{
for (int j = 0; j < M.GetLength(1); j++)
{
Console.Write(M[i,j] +" ");
}
Console.WriteLine();
}
DONG NAI UNIVERSITY OF TECHNOLOGY
Arrays of Arrays
When we want to create a multi Array with different size:
- The first, we declare size of row, so each row will hold an array with any
size.
- The second, We will declare those array
- And Initialize value for each item in array
DataType[ ][ ] arrName = new DataType[Size][ ];
DONG NAI UNIVERSITY OF TECHNOLOGY
int[][] K = new int[3][];
K[0] = new int[] { 1,3};
K[1] = new int[] { 9};
K[2] = new int[] { 5, 4, 9 };
for (int i = 0; i < K.GetLength(0); i++)
{
for (int j = 0; j < K[i].Length; j++)
{
Console.Write(K[i][j] +" ");
}
Console.WriteLine();
}
DONG NAI UNIVERSITY OF TECHNOLOGY
Array Class
Provides methods for creating, manipulating, searching,
and sorting arrays, thereby serving as the base class for all
arrays in the common language runtime.
DONG NAI UNIVERSITY OF TECHNOLOGY
Method
CreateInstance(Type, Int32 [])
Creates a multidimensional Array of the specified Type and dimension
lengths, with zero-based indexing. The dimension lengths are specified in an
array of 32-bit integers.
DONG NAI UNIVERSITY OF TECHNOLOGY
// Creates and initializes a new two-dimensional Array of type
Int32.
(2 rows, 3 cols)
Array myArr = Array.CreateInstance(typeof(Int32),2, 3);
for (int i = myArr.GetLowerBound(0);
i <= myArr.GetUpperBound(0); i++)
for (int j = myArr.GetLowerBound(1);
j <= myArr.GetUpperBound(1); j++)
{
myArr.SetValue(i+j, i, j);
}
DONG NAI UNIVERSITY OF TECHNOLOGY
TO get value foreach item:
for (int i = myArr.GetLowerBound(0);
i <= myArr.GetUpperBound(0); i++)
{
for (int j =
myArr.GetLowerBound(1);
j <= myArr.GetUpperBound(1); j++)
{
Console.Write(
myArr.GetValue(i, j) + " ");
}
Console.WriteLine();
}
DONG NAI UNIVERSITY OF TECHNOLOGY
Method
Description
Copy(Array, Array,
Copies a range of elements from an Array starting at
Int32)
the first element and pastes them into another Array
starting at the first element. The length is specified as
a 32-bit integer
DONG NAI UNIVERSITY OF TECHNOLOGY
int[] myArr = new int[5] { 1, 2, 3, 4, 5 };
Object[] myObjArr = new Object[5] { 26, 27, 28, 29, 30 };
Copy 2 element from myArr to myObjArr:
Array.Copy(myArr,myObjArr,2) ;