• 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
Trang 2Multi Dimension Array
Trang 35 2 9 7 6 0 8
- Same name
- Same type
- Get Element by Index ( 0 n-1)
One Dimension Array
Trang 4• 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
Declaring & Creating Arrays
Trang 6int M1 [] =new int [ 10 ] ;
for ( int i=0;i<M1.Length;i++)
Trang 7• 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
Properties & Methods
Trang 8Passing Arrays to Method
• Pass arrays as arguments to methods by specifying the name of the array (no brackets)
• Individual array elements are passed by value
Trang 9static void inputArray(int[] arr)
{
Random ran = new Random();
arr[i] = ran.Next(100);
}
Passing Arrays to Method
Trang 10Using out or ref keyword to pass by reference
static void edit( out int n)
Trang 11The out parameter must be assigned to before control leaves the current method
static void edit(out int n)
{
//n = -113;
}
Compile error
Trang 12edit( out arr[0]);
edit( ref arr[0]);
Two ways: arr[0] -113
Use out keyword:
Use ref keyword:
arr[0]=100;
Trang 15arrName = 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} };
Trang 16GetLength(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
Trang 17for ( int i = 0; i < M.GetLength(0); i++)
Trang 18Arrays 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][ ];
Trang 19int [][] K = new int [3][];
Trang 20Array Class
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the base class for all arrays in the common language runtime.
Trang 21CreateInstance(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
Trang 22// 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);
Trang 23TO get value foreach item:
for ( int i = myArr.GetLowerBound(0);
Trang 24a 32-bit integer
Trang 25int[] 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) ;
Trang 26Searches an entire one-dimensional sorted Array for a specific element, using the
object ( not found =-1)
int[] M = new int[5] { 1, 2, 3,
4, 5 };
int found=Array.BinarySearch(M, 8);
Trang 27Method Description
using the IComparable implementation of each element of the Array
int[] M1 = new int[]{1,9,2,7,8};
Array.Sort(M1);
Out put: 1 2 7 8 9
Trang 28ArrayList Class
dynamically increased as required
Trang 29Constructors Description
that is empty and has the default initial capacity
ArrayList arrList = new
Trang 30Constructors Description
that is empty and has the specified initial capacity
ArrayList arrList = new
Trang 31Initializes a new instance of the ArrayList class that contains elements copied from the specified collection and that has the same initial capacity
as the number of elements copied
ArrayList arr1=new ArrayList();
arr1.AddRange( new int [] { 1, 1, 4 });
ArrayList arr2 = new ArrayList(arr1);
Trang 32Method Description
Add Adds an object to the end of the ArrayList
ArrayList arr=new ArrayList();
arr.Add(1);arr.Add( "a" );arr.Add( 9 );
foreach (object item in arr)
Console.Write(item+“ ”);
//Output : 1 a 9
Trang 33Method Description
ArrayList
ArrayList arr=new ArrayList();
arr.AddRange( new int [] { 1, 1, 4 });
foreach (int value in arr)
Console.Write(value+" ");
//Output : 1 1 4
Trang 34Method Description
index.
ArrayList arr=new ArrayList();
arr.AddRange( new int [] { 1, 1, 4 });
arr.Insert(1, "teo" );
foreach (object value in arr)
Console.Write(value+" ");
//Output : 1 teo 1 4
Trang 35InsertRange Inserts the elements of a collection into the ArrayList
at the specified index
ArrayList arr= new ArrayList();
arr.AddRange( new int [] { 1, 1, 4 });
ArrayList arrIns= new ArrayList();
Trang 36Remove Removes the first occurrence of a specific object from the
Trang 38RemoveRange Removes a range of elements from the ArrayList
ArrayList arr = new ArrayList();
arr.AddRange(new object[]
{ 1, "teo", 4, "ti" });
arr.RemoveRange(1, 2);
//1 is Start Index to remove
//2 is number of element to remove
foreach ( object value in arr)
Console Write(value + " " );
//Output : 1 ti
Trang 40Method Description
ArrayList arr=new ArrayList();
Trang 41Contains Determines whether an element is in the ArrayList
arr.AddRange( new string [] { "Chi" , "Pheo" , "Thi" , "No" });
bool bFound = arr.Contains("No");
Trang 42IndexOf Searches for the specified Object and returns the zero-based
index of the first occurrence within the entire ArrayList
ArrayList arr = new ArrayList ();
arr.AddRange( new string [] { "Chi" , "Pheo" , "Thi" , "No" });
int pos1 = arr.IndexOf("Pheo" );
Console WriteLine( "Pos1=" +pos1);
//Output : Pos1=1
int pos2 = arr.IndexOf(“Teo" );
Console WriteLine( "Pos2=" +pos2);
//Output : Pos2=-1
Trang 43Method Description
index of the last occurrence within the entire ArrayList
arr.AddRange(new string[] { “Teo", "Pheo", "Thi",
“Teo" });
int pos = arr.LastIndexOf(“Teo");
Console.WriteLine("Pos="+pos);
//Output : Pos=3
Trang 44Sort Sorts the elements in the entire ArrayList
ArrayList arr = new ArrayList();
Trang 45BinarySearch Searches the entire sorted ArrayList for an element using the
default comparer and returns the zero-based index of the element
ArrayList arr = new ArrayList();
Trang 46Clone Creates a shallow copy of the ArrayList
ArrayList arr = new ArrayList();
Trang 47What’s happen when We use Clone method???
ArrayList arr = new ArrayList();
Trang 48CopyTo Copies the entire ArrayList to a compatible one-dimensional
Array, starting at the beginning of the target array
arr.Add(1);arr.Add( "a" );arr.Add( 9 );
object []M1= new object [arr.Count];
foreach ( object val in M1)
Console Write(val + " " );
//Output : 1 a 9
Trang 49Method Description
arr.Add(1);arr.Add( "a" );arr.Add( 9 );
arr.Reverse();
foreach ( object val in M1)
Console Write(val + " " );
//Output : 9 a 1
Trang 50Dictionary< TKey , TValue >
Represents a collection of keys and values
Trang 51 Each addition to the dictionary consists of a value
and its associated key.
close to O(1).
Trang 52 Every key in a Dictionary <TKey, TValue > must be
unique according to the dictionary's equality
comparer
A key cannot be null , but a value can be, if the value
type TValue is a reference type
Trang 53 The capacity is automatically increased as required
by reallocating the internal array
Use KeyValuePair <TKey, TValue > structure representing
a value and its key.
Trang 54Dictionary <TKey, TValue >()
Description:
> class that is empty, has the default initial capacity, and
uses the default equality comparer for the key type
Trang 55// Create a new dictionary of ArrayList, with string keys.
Dictionary < string , ArrayList >
Dictionary < string , ArrayList >();
Trang 56// Or
);
Trang 57Add ( TKey key, TValue value )
Description:
Adds the specified key and value to the dictionary.
arr.Add("Nhà");arr.Add("Chỗ ở");
Trang 59ArrayList aItem= myDIC["Home"];
foreach ( KeyValuePair<string,
{
string strKey = item.Key;
}
KeyValuePair <TKey, TValue>
Defines a key/value pair that can be set or retrieved
Trang 61public bool ContainsKey (TKey key)
Description:
contains the specified key
if (myDIC.ContainsKey("Home"))
{
MessageBox.Show(“Có home");
}
Trang 62public bool ContainsValue
(TValue value )
Description:
contains a specific value
if (myDIC.ContainsValue(arr))
{MessageBox Show( "Có Arr này rồi" );
}
Trang 63public bool TryGetValue
(TKey key, out TValue value )
Description:
Gets the value associated with the specified key
ArrayList arr = null ;
if (myDIC.TryGetValue( "Home" , out arr))
{ //Process arr here
}
else { //Could not Get value
}
Trang 64public bool Remove (TKey key )
Description:
Removes the value with the specified key from the
Dictionary <TKey, TValue>.
myDIC Remove ( "Home" );
Trang 65Count
Description:
Gets the number of key/value pairs contained in the
Dictionary <TKey, TValue >
Trang 66Item ( indexer)
Description:
Gets or sets the value associated with the specified key
KeyNotFoundException=>The property is retrieved and key does not exist in
the collection
ArgumentNullException=>key is null
myDIC["Home"]=arr (Arraylist);
Trang 67Description:
Gets a collection containing the keys in the
myDIC.Keys;
foreach (string s in keyColl)
}
Trang 68Description:
Gets a collection containing the values in the
myDIC.Values;
foreach(ArrayList arr in valColl)
{//Process value here
}
Trang 69END