Data Structure: ITE 222 Computer Programming 2
Data Structure: ITE 222 Computer Programming 2
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
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
www.tutorialspoint.com/cshape
www.dotnetperls.com/