Tải bản đầy đủ (.pptx) (69 trang)

Bài giảng Lập trình C# 2010: Chương 4 - ĐH Công nghệ Đồng Nai

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (873.21 KB, 69 trang )

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]

M[2]

M[3]

M[4]

M[5]

M[6]

2

9

7

6

0

8

- Same name
- Same type
- Get Element by Index ( 0 n-1)



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:
Creating:

<DataType> [ ] <ArrayName>;
int [ ] M;
Button[ ] arrButton;

<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;iM1[i]=i*2;
for(int i=0;iConsole.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 in p u tA rray(int[] arr)
{
Random ran = new Random ();
for (int i= 0; i< arr.Length; i+ + )

arr[i] = ran.N ext(100);
}


DONG NAI UNIVERSITY OF TECHNOLOGY

Using out or ref keyword to pass by reference
static void ed it(ou t in t n )
{
n = -113;
}

OR:


static void ed it(ref in t 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;
U se ou t keyw ord :
ed it(ou t arr[0]);
U se ref keyw ord :
ed it(ref arr[0]);
Tw o w ays: arr[0] -113


DONG NAI UNIVERSITY OF TECHNOLOGY

7

9
8

2 9 0
5 4 1
0 3 6
Multi
Dimension


Column 0
Row 0

Row 1

Row 2

Column 1

DONG NAI UNIVERSITY OF TECHNOLOGY

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

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

G etLength(int dim ension)
int[,] M = {{1,8},{2,5},{1,1} };
row = M .G etLength(0); row = 3
col= M .G etLength(1); col= 2
int[, ,] M 2 = new Int32[3, 4, 5];
x= M 2.G etLength(0);x=
3
y= M 2.G etLength(1);y=
4
z= M 2.G etLength(2);z=


DONG NAI UNIVERSITY OF TECHNOLOGY

Examples
for (in t i = 0; i < M .G etLen g th (0); i+ + )

{
for (in t j = 0; j < M .G etLen g th (1); j+ + )

{
C on sole.W rite(M [i,j] + " ");
}
C on sole.W riteLin e();
}



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.G etLength(0); i+ + )
{
for (int j= 0; j< K[i].Length; j+ + )
{
Console.W rite(K[i][j] + " ");
}
Console.W riteLine();
}


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 tw odim ensionalArray of type Int32.
(2 row s, 3 cols)
Array m yArr =
Array.C reateIn stan ce(typeof(Int32),2, 3);

for (int i= m yArr.G etLow erBound(0);
i< = m yArr.G etU pperBound(0); i+ + )
for (int j= m yArr.G etLow erBound(1);
j< = m yArr.G etU pperBound(1); j+ + )
{

m yArr.SetValue(i+ j, i, j);
}


DONG NAI UNIVERSITY OF TECHNOLOGY

TO get value foreach item :
for (int i= m yArr.G etLow erBound(0);
i< = m yArr.G etU pperBound(0); i+ + )
{
for (int j=
m yArr.G etLow erBound(1);
j< = m yArr.G etU pperBound(1); j+ + )
{
Console.W rite(
m yArr.G etV alu e(i, j) + " ");
}
Console.W riteLine();
}


DONG NAI UNIVERSITY OF TECHNOLOGY

Method
Description
Copy(Array, Copies a range of elements
Array, Int32) from an Array starting at 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[] m yArr = new int[5] { 1, 2, 3, 4,
5 };
O bject[] m yO bjArr = new O bject[5]
{ 26, 27, 28, 29, 30 };
Copy 2 elem ent from m yArr to m yO bjArr:

Array.C op y(m yArr,m yO bjArr,2) ;


×