Sei sulla pagina 1di 25

DATA STRUCTURE

ITE 222 Computer Programming 2


Data Structure-Array
 Array
 An array is a data structure that contains a number of
variables called the elements of the array.
 Always start at zero
 Pass by reference
 Has 3 types with general forms:
 One- Dimension Array Type [] var-array
 Multi- Dimension Array Type [..,..,...] var-array
 Jagged Array Type [][] var-array
Data Structure-Array(cont.1)
 When declare array for reserved memory:
 One- Dimension Array
 We use type [] var-array = new type [size of array]
 Example: declare an array of five integers as in the following
int []num = new int [5];
 Multi- Dimension Array
 We use type [..,..,...] var-array = new type [size1, size2,…]
 Example: the following declaration creates an array of three
dimensions, 4, 2, and 3:
int [, ,]num = new int [4,2,3];
Data Structure-Array(cont.2)
 Jagged Array
 We use type [][] var-array = new type [][];
 Example: a declaration of a single-dimensional array that has
three elements, each of which is a single-dimensional array of
integers:
int [][]num = new int [3][]
Data Structure-Array(cont.3)
 Initializing Arrays
 C# provides simple and straightforward ways to initialize arrays
at declaration time by enclosing the initial values in curly braces
({}).
 Example
int[] numbers = new int[5] {1, 2, 3, 4, 5};
Or int[] numbers = new int[] {1, 2, 3, 4, 5};
Or int[] numbers = {1, 2, 3, 4, 5};

string[] names = new string[ ] {“Bird", “Rat", “Chicken"};


Or string[] names = new string[] {“Bird", “Rat", “Chicken"};
Or string[] names = {“Bird", “Rat", “Chicken"};

string [,] name = new string[,]{ {“Apisit”, “Mark”}, {“Taksin”, “Takky”}};


Data Structure-Array(cont.4)
 Example

string [,] name = new string[,]{ {“Apisit”, “Mark”}, {“Taksin”, “Takky”}};


Or string [,] name = new string[2,2]{ {“Apisit”,”Mark”}, {“Taksin”,
‘Takky”}};
Or string [,] name = { {“Apisit”,”Mark”}, {“Taksin”, “Takky”}};

int[][] numbers = new int[][] { new int[] {3,4}, new int[] {6,7,8,9} };
Or int[][] numbers = new int[2][] { new int[] {3,4}, new int[] {6,7,8,9} };
Or int[][] numbers = { new int[] {3,4}, new int[] {6,7,8,9} };
Data Structure-Array(cont.5)
 Accessing Array Members
 Accessing
array members is straightforward and similar
to how you access array members in C/C++.
 Example:
 int []numberF = new int[6]{15, 26, 30, 4, 12, 92}
 Ifwe need to rearrange value the 5th {numberF[4] }element
from number 12 to number 9, we do..
 numberF[4] = 9;
Data Structure-Array(cont.6)
 Some useful Array properties
 Length:uses the Length property to get the length of a
new array.
 cannot assign Length.
 Example:
{
string [] name = new string [10];
Console.WriteLine(name.Lenght); //10
}
 IsFixedSize : Gets a value indicating whether the Array
 LongLength : Gets a 64-bit integer that represents the total
number of elements in all the dimensions of the Array.
Data Structure-Array(cont.7)
 Some useful Array methods
 GetLength() : uses to get the length of a new dimension
array.
 Example:
int[,] two = new int[6, 20];
Console.WriteLine(two.GetLength(0)); // Writes 6
Console.WriteLine(two.GetLength(1)); // Writes 20
 Clear() : Sets a range of elements in the Array to zero,
to false, or to null, depending on the element type.
Data Structure-Array(cont.8)
 Sort(Array):Sorts the elements in an entire one-
dimensional Array using the IComparable
implementation of each element of the Array.
 In pattern: Array.Sort();
 Reverse(Array): Reverses the sequence of the elements
in the entire one-dimensional Array.
 In pattern: Array. Reverse();
Data Structure-Array(cont.9)
 Example:
Use for-loop to call output elements in an array
string []name = { John, Anny, Robert, Boppy};
for( int i = 0; i < name.lenght ; i++){
console.writeline(“Name[{0}]: {1}”,i ,name[i]);
}
console.read();
Data Structure-Array(cont.10)
 Example:
Use for each loop to output elements in an array
string []name = { John, Anny, Robert, Boppy};
foreach( string txt in name ){
console.writeline(“name: {0}”,txt);
}
console.read();
Data Structure-Struct
 Struct Variable
 The struct (short for structure) is just that. That is, structs
are data structures are composed of several pieces of
data, possibly of different types. They enable you to
define your own types of variables based on this
structure.
 in general form:
struct <typeName> { <memberDeclarations> }
Data Structure-Struct(cont.1)
 Example:
declare both name_employee in string and ID in int
struct EMPinfo{
public sting name_Employee;
public int codeID;
}
class Program {
static void Main(string[] args) {
EMPinfo emp_A;
emp_a.name_Employee = “Kritseda”;
emp_a.codeID = 10252;
}
Data Structure- ArrayList
 ArrayList
is a collection like array
 Once you set the array size, you can't change it
string [] name;
name = new string[10];
name = new string[12];
 With ArrayList, you can add and remove elements and
the ArrayList variable will automatically resize itself
accordingly.
 It often use for C# old style.
Data Structure- ArrayList(cont.1)
 How to declare ArrayList: we use ..
ArrayList var-ArrayList = new ArrayList();

 Example:
ArrayList arrayA = new ArrayList();
 Use ‘Add()’ method to append new element at the end
of the ArrayList
 Example: add 1,7,8 in arrayA
ArrayList arrayA = new ArrayList();
arrayA.Add(1);
arrayA.Add(7);
arrayA.Add(8);
Data Structure- ArrayList(cont.2)
 Use‘Count’ property to show number of elements in
ArrayList. ‘Count’ property will return int datatype
 Example: add 1,7,8 in arrayA
ArrayList arrayA = new ArrayList();
arrayA.Add(1);
arrayA.Add(7);
arrayA.Add(8);
Console.WriteLine(“ArrayList size:{0}”,arrayA.count)
 Use ‘Clare()’ method to clear all elements in ArrayList.
arrayA.Clare();
Data Structure- ArrayList(cont.3)
 We can pass ArrayList as Object in example, we send
ArrayList Object to another function.
 Example:
static void main(){
ArrayList arrayA = new ArrayList();
arrayA.Add(1);
arrayA.Add(7);
Display(arrayA);
}
static void Display(ArrayList list) {
foreach (int i in list) { Console.WriteLine(i); }
}
Data Structure- ArrayList(cont.4)
 Use ‘AddRange’ method to combine second ArrayList
into first ArrayList
 Example:
static void Main(string[] args){
ArrayList arrayA = new ArrayList();
arrayA.Add(1);
arrayA.Add(7);
ArrayList arrayB = new ArrayList();
arrayB.Add(12);
arrayA.AddRange(arrayB);
foreach (int i in arrayA) { Console.WriteLine(i); }
}
Data Structure- ArrayList(cont.5)
 Use ‘Sort/Reverse’ method to provide a new view of
the sorting in ArrayList
 Example:
static void Main(string[] args){
ArrayList arrayA = new ArrayList();
arrayA.Add(15);
arrayA.Add(7);
arrayA.Add(12);
Console.WriteLine("Display arraylist:");
foreach (int i in arrayA) { Console.WriteLine(i); }
arrayA.Sort(); // arrayB.Reverse();
Console.WriteLine("Display arraylist after sorting:");
foreach (int i in arrayA) { Console.WriteLine(i); }
Console.ReadKey();
}
Data Structure- ArrayList(cont.6)
 For more example: insert, RemoveAt in ArrayList

static void Main(string[] args)


{
ArrayList list = new ArrayList();
list.Add(“I");
list.Add(“have");
list.Add(“100 baht");
list.RemoveAt(1); // It becomes [have, 100 baht]
list.Insert(0, “Supot"); // It becomes [Supot,have, 100
baht] //
// Remove first two words from ArrayList. //
list.RemoveRange(0, 2); // Display the result ArrayList. //
foreach (string value in list)
{ Console.WriteLine(value); // <-- “100 baht“}
}
Data Structure- Generic Class
 Generic classes have type parameters. Separate
classes, each with a different field type in them, can
be replaced with a single generic class.
 one amazing feature in the C# language is the
ability to specify and use generic types.
Data Structure- Generic Class(cont.1)

 Example
class Test<T> {
T _value;
public Test(T t) { // The field has the same type as the
parameter. this._value = t;
}
public void Write() { Console.WriteLine(this._value); }
}
Data Structure- Generic Class(cont.2)

class Program {
static void Main() { // Use the generic type Test with an int
type parameter.
Test<int> test1 = new Test<int>(120);
// Call the Write method.
test1.Write();
// Use the generic type Test with a string type parameter.
Test<string> test2 = new Test<string>("Maggies");
test2.Write();
}
}
References

 Today’s slides are compiled from the following:


 Official website of MSDN-Microsoft
 www.msdn.microsoft.com/en-us/library/kx37x362.aspx

 www.tutorialspoint.com/cshape
 www.dotnetperls.com/

 ศุภชัย สมพานิ ช. คูม่ อ


ื เรียนและใช ้งานภาษา Visual C#. กรุงเทพฯ
: สวัสดี ไอที, 2556

Potrebbero piacerti anche