YOMEDIA
Lecture Windows programming: Chapter 6 - Châu Thị Bảo Hà
Chia sẻ: Kiếp Này Bình Yên
| Ngày:
| Loại File: PPTX
| Số trang:69
52
lượt xem
3
download
Download
Vui lòng tải xuống để xem tài liệu đầy đủ
Chapter 6 of lecture Windows programming introduce about Arrays - collections. In this chapter you will be learn contents: Declaring and Allocating arrays, initializing an array, properties – methods of an array, foreach loops, System.Collections namespace, ArrayList class,...
AMBIENT/
Chủ đề:
Nội dung Text: Lecture Windows programming: Chapter 6 - Châu Thị Bảo Hà
- Arrays Collections
Chapter 6
Ebook: Beginning Visual C# 2010, part 1, chapter 5, 11
Reference: CSharp How to Program
- Contents
Arrays
Collections
Slide 2
- Arrays (p.110)
Declaring and Allocating arrays
Initializing an array
Properties – Methods of an array
foreach loops
Array parameter
Multidimensional arrays
Slide 3
- Declaring and Allocating arrays
Arrays are indexed lists of variables stored in a single
array type variable
DataType[ ] ArrayName; // declare array
ArrayName = new DataType[size]; // allocate array
// declare and allocate array
DataType[ ] ArrayName = new DataType[size];
Example: int[] a = new int[12];
bool[] b = new bool[100];
Circle[] listCir = new Circle[5]; // reference types
When arrays are allocated, the elements are initialized:
zero for the numeric data-type
false for bool variables
null for reference types Slide 4
- Initializing an array
Arrays can be declared, allocated, initialized in a
statement
int[] myIntArray = new int[5] { 2, 4, 6, 8, 10};
Allocate space for the array – number of elements in
initializer list determines the size of array
int[] myIntArray = { 2, 4, 6, 8, 10};
Slide 5
- Properties Methods of an array
array_name.Length
returns size of array
array_name.GetValue (int index)
array_name[int index]
returns an object at position index
array_name.SetValue (object value, int index)
array_name[int index] = value
modifies an object at position index
Slide 6
- foreach Loops
foreach ( in )
{
// can use for each element
}
Example:
int[] myArray = { 2, 4, 6, 8, string[] friendNames = { "Robert", "Mike",
10}; "Jeremy" };
foreach ( int x in myArray ) foreach (string friendName in friendNames)
{
Console.WriteLine( x ); Console.WriteLine( friendName );
Circle[] listC = new Circle[3] }{ new Circle(5.0), new Circle(7.0), new
Circle(9.0) }
foreach (Circle c in listC)
{
Console.WriteLine(c.Radius + " : " + c.getArea();
} Slide 7
- Example
string output = "";
const int ARRAY_SIZE = 10;
int[] x = new int[ 10 ];
int[] y = { 32, 27, 64, 18, 95, 14, 90, 70, 60, 37 };
int[] z;
z = new int[ ARRAY_SIZE ];
for ( int i = 0; i < z.Length; i++ )
z.SetValue(2 + 2 * i, i);
output += "Subscript\tArray x\tArray y\tArray z\n";
for ( int i = 0; i < ARRAY_SIZE; i++ ) Change
output += i + "\t" + x.GetValue(i) + "\t" + y.GetValue(i) + for to
"\t" + z.GetValue(i) + "\n"; foreach
loop?
MessageBox.Show( output, "Initializing an array of int values",
MessageBoxButtons.OK, MessageBoxIcon.Information );
Slide 8
- Array parameter
No need the number of elements in the parameter
array when you define the method
Example:
void InputArray (int[] a)
void OutputArray (int[] a)
int Sum (int[] arr)
bool Search (int[] arr, int key)
Pass arrays as arguments to methods by specifying
the name of the array (no brackets)
Example: InputArray(a);
Arrays are passed by reference
Individual array elements are passed by value
Slide 9
- Array parameter: Example
private void showOutputButton_Click( object sender, System.EventArgs e )
{
int[] a = { 1, 2, 3, 4, 5 };
for ( int i = 0; i < a.Length; i++ )
outputLabel.Text += " " + a[ i ];
ModifyArray( a ); // passed by reference
outputLabel.Text += "\n";
for ( int i = 0; i < a.Length; i++ )
outputLabel.Text += " " + a[ i ];
ModifyElement( a[ 3 ] );
outputLabel.Text += "\na[ 3 ] after ModifyElement: " + a[ 3 ];
}
public void ModifyArray( int[] b )
{
Slide
- Multidimensional Arrays (p.113)
Require two or more subscripts to identify a particular
element
Arrays that require two subscripts to identify an
element are called double-subscripted arrays
rectangular arrays
often represent tables
jagged arrays (arrays of arrays)
arrays that compose jagged arrays can be of different lengths
Slide
- Rectangular arrays
Column 0 Column Column 2 Column 3
1
Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3]
Row 1 a[1, 0] a[1, 1] a[1, 2] a[1, 3]
Row 2 a[2, 0] a[2, 1] a[2, 2] a[2, 3]
Column index (or subscript)
Row index (or subscript)
Array name
Slide
- Rectangular arrays (cont.)
Declaring – Allocating:
DataType[,] ArrayName;
ArrayName = new DataType [rowSize, colSize];
DataType[,] ArrayName = new DataType [rowSize,
colSize];
Example:
int[,] b = new int[ 2, 2 ];
b[ 0, 0 ] = 1; b[ 0, 1 ] = 2;
b[ 1, 0 ] = 3; b[ 1, 1 ] = 4;
or:
int[,] b = { { 1, 2 }, { 3, 4 } };
GetLength (int x): returns size of xth dimension in a
array Slide
- Jagged arrays (arrays of arrays)
Column 0 Column Column 2 Column 3
1
Row 0 a[0, 0] a[0, 1] a[0, 2] a[0, 3]
Row 1 a[1, 0] a[1, 1]
Row 2 a[2, 0] a[2, 1] a[2, 2]
Column index (or subscript)
Row index (or subscript)
Array name
Slide
- Jagged arrays (cont.)
Declaring – Allocating:
DataType[ ][ ] ArrayName;
ArrayName = new DataType[Size][ ];
DataType[ ][ ] ArrayName = new DataType[Size][ ];
Example:
int[ ][ ] c = new int[2][ ];
c[0] = new int[] { 1, 2 };
c[1] = new int[] { 3, 4, 5 };
Length property: returns number of rows or columns
c.Length 2
c[1] .Length 3
Slide
- Rectangular arrays: Example
Slide
- Jagged arrays: Example
Slide
- Example
Slide
- Contents
Arrays
Collections
Slide
- Collection classes
Collection classes are used for maintaining lists of
objects, and they may expose more functionality than
simple arrays
Collection classes are defined as part of
the System.Collections or System.Collections.Generic
namespace
the collection classes in the System.Collections allow to
create untyped collections
store any type of object in the collection
the collection classes in the System.Collections.Generic
allow to create typed collections
only store the specified data type
the data type is specified in angle bracket () after name of the
collection class Slide
Thêm tài liệu vào bộ sưu tập có sẵn:
Báo xấu
LAVA
ERROR:connection to 10.20.1.98:9315 failed (errno=111, msg=Connection refused)
ERROR:connection to 10.20.1.98:9315 failed (errno=111, msg=Connection refused)
Đang xử lý...