IntegratedMulti TrophicAquaculture Systems Aquaculture experts Forum.pdf
Chapter 2: array and array list data structure
1. DATA STRUCTURE
Chapter 2: Array & Array List
Prepared & Presented by
Mr. Mahmoud R. Alfarra
2011-2012
College of Science & Technology
Dep. Of Computer Science & IT
BCs of Information Technology
http://mfarra.cst.ps
2. Out Line
Print the elements of Arrays
Storing data in Arrays
Swap two elements in array.
How to delete an element from array?
How to insert a new element in array
The concept of complexity
What is Array List? How to use?
2
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
5. Print the elements of 1-D Arrays
5
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. string[] names = { "Ali", "Mahmoud", "Hussam",
"Ala" };
4. for (int i = 0; i < names.Length; i++)
5. Console.WriteLine("The next name is
"+names[i]);
6. Console.Read();
7. }
6. Print the elements of 2-D Arrays
6
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. string[,] names = { {"Ali", "Mahmoud"}, {"Hussam", "Ala"} };
4. for (int i = 0; i < names.GetLength(0); i++)
5. Console.WriteLine("The next name is " + names[i,0]+" “ + names[i,1]);
6. Console.Read();
7. }
7. Storing data in 1-D Arrays
7
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. int[] id = new int[5];
4. for (int i = 0; i < 5; i++)
5. id[i] =int.Parse(Console.ReadLine());
6. Console.WriteLine("==== The numbers in Array are =====");
7. for (int i = 0; i < 5; i++)
8. Console.WriteLine(" Next Number is " + id[i]);
9. Console.ReadLine();
10. }
8. Storing data in 2-D Arrays
8
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. string[,] names = new string[2, 3];
4. for (int i = 0; i < 2; i++)
5. for (int j = 0; j < 3; j++)
6. names[i, j] = Console.ReadLine();
7. for (int i = 0; i < 2; i++)
8. {
9. for (int j = 0; j < 3; j++)
10. Console.Write(names[i, j]);
11. Console.WriteLine();
12. }
13. Console.Read();
14. }
9. Swap two elements in 1-D array
9
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. int[] numbers = { 3, 5, 7, 23, 56, 12 };
4. int x;
5. x = numbers[0];
6. numbers[0] = numbers[1];
7. numbers[1] = x;
8. Console.WriteLine(" ");
9. Console.WriteLine(" The elements of array after swap");
10. for (int i = 0; i < numbers.Length; i++)
11. Console.WriteLine(" "+numbers[i]);
12. Console.Read();
13. }
Try to implement the above example with 2-D array
11. How to delete an element from
array?
11
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. int[] id = { 10, 12, 11, 45 };
4. // to delete the value 12
5. int i;
6. for ( i = 0; i < id.Length-1; i++)
7. {
8. if (id[i] == 12)
9. {
10. break;
11. } }
12. for (int j =i ; j < id.Length-1; j++)
13. id[j] = id[j + 1];
14. for (int k = 0; k < id.Length; k++)
15. Console.WriteLine(" "+id[k]);
16. Console.Read(); }
12. How to delete an element from
array?
12
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. int[] id = { 10, 12, 11, 45 };
4. // to delete the value of index 2
5. for (int i =2 ; i < id.Length-1; i++)
6. id[i] = id[i + 1];
7. for (int k = 0; k < id.Length; k++)
8. Console.WriteLine(" "+id[k]);
9. Console.Read();
10. }
22. How to use Array List?
22
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. ArrayList names = new ArrayList();
4. int po1 = names.Add("Ali");
5. int po2 = names.Add("hussam");
6. int po3 = names.Add("hassan");
7. int po4 = names.Add("Alaa");
8. int po5 = names.Add("wael");
9. Console.WriteLine();
10. Console.WriteLine(" Position of PO1: " + po1 +
11. " Position of PO2: " + po2 + " Position of PO5: " + po5);
12. Console.WriteLine(" number of names in the ArrayList: " +
names.Count);
13. Console.ReadLine();
14. }
Instance variable in
Array List to return
the number of items
in.
23. How to use Array List?
23
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. ArrayList list1 = new ArrayList();
4. list1.Add(40);
5. list1.Add(50);
6. list1.Add(60);
7. list1.Add(70);
8. ArrayList list2 = new ArrayList();
9. list2.Add(80);
10. list2.Add(90);
11. list1.AddRange(list2);
12. Console.WriteLine(" === The elements of list1 are ===");
13. foreach (object x in list1)
14. Console.WriteLine(" "+x);
15. Console.ReadLine();
16. }
24. How to use Array List?
24
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. ArrayList list1 = new ArrayList();
4. list1.Add(40);
5. list1.Add(50);
6. list1.Add(60);
7. list1.Add(70);
8. ArrayList list2 = new ArrayList();
9. list2 = list1.GetRange(0, 3);
10. Console.WriteLine();
11. foreach (object x in list2)
12. Console.WriteLine(" "+x);
13. Console.ReadLine();
14. }
25. How to use Array List?
25
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
1. static void Main(string[] args)
2. {
3. ArrayList list1 = new ArrayList();
4. list1.Add(40);
5. list1.Add(30);
6. list1.Add(10);
7. list1.Add(100);
8. list1.Sort();
9. Console.WriteLine();
10. foreach (object x in list1)
11. Console.WriteLine(" " + x);
12. Console.ReadLine();
13. }
26. Thank you …
26
البيانات تراكيب مساق
إعداد العلمية المادة
/
أ
.
ا َّالفــر رفيق محمود
Remember that: question is the key of knowledge