Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER.

Similar presentations


Presentation on theme: "Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER."— Presentation transcript:

1 Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY (CST) KHANYOUNIS- PALESTINE

2 و من يتقِ الله... قال ربي سبحـانه : و لو أن أهل القرى أمنوا و اتقوا لفتحنا عليهم بركات من السماء و الأرض. Mahmoud Rafeek Alfarra 2 Downloaded from http://staff.cst.ps/mfarra شريحـة ثابتـة لعلنا نحسن من خلالها أخلاقنـا و أفعالنا لنفوز يوم الامتحان الحقيقي

3 Out Lines Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 3 Revision about Array data structure Example: Store the even elements in array What is Multidimensional Arrays? Applications !! Example: X O’s Project.

4 Revision about Array data structure Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 4 An array is a special type of object that can hold an ordered collection of elements. The type of the elements of the array is called the base type of the array; the number of elements it holds is a fixed attribute called its length. Java supports arrays of all primitive and reference types.

5 Revision about Array data structure Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 5 Arrays are static data structure (which means ?!!).

6 Revision about Array data structure Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 6 To Create & initialize the one dim. array: basetype [] arrayname = new basetype [length]; Arrayname[0] = val1; Arrayname[1] = val2; Arrayname[…] = val n ; Or basetype [] arrayname = { val1, val2, val3, … };

7 Revision about Array data structure Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 7 Examples: int [] salary = new int [3]; salary[0] = 487; salary[1] = 600; salary[2] = 894; Or int [] salary = { 487, 600, 894 };

8 Example : Store even elements in array Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 8 Using Procedural programming, Write a program to store the even numbers from int To int in array. Then, print the summation of the array’s elements.

9 Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 9 import javax.swing.JOptionPane; public class SumEvenArray { public static void even (int low, int high){ int size = (high - low)/2 +2; int [] evenarr = new int [size]; int len=0; for (int i = low; i<=high; i++) { if (i%2==0){ evenarr[len] = i; len++;} } sumarr(evenarr); } public static void sumarr(int [] evenarr) { int sum=0; for (int i =0; i<evenarr.length; i++) sum= sum+evenarr[i]; JOptionPane.showMessageDialog(null, "Sum is: "+sum); } public static void main(String[] args) { even(100, 110); } }

10 What is Multidimensional Arrays? Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 10 Multidimensional arrays with two dimensions are often used to represent tables of values consisting of information arranged in rows and columns. To identify a particular table element, we must specify two indices. By convention, the first identifies the element's row and the second its column.

11 What is Multidimensional Arrays? Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 11 To Create & initialize the two dim. array: basetype [] [] arrayname = new basetype [r][c]; Arrayname[0] [0]= val1; Arrayname[0] [1] = val2; Arrayname[…][…] = val n ; Or basetype [] [] arrayname = { {val1, val2}, {val3, …} };

12 What is Multidimensional Arrays? Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 12 To Create & initialize the two dim. array: int [] [] table = new int [3][2]; table[0] [0]= 3; table[0] [1] = 5; table[0] [2] = 4; table[…][…] = val n ; Or int [] [] table = { {3, 5, 4}, {valn, …} };

13 What is Multidimensional Arrays? Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 13 Java does not support multidimensional arrays directly, but it does allow the programmer to specify one-dimensional arrays whose elements are also one-dimensional arrays.

14 Example : Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 14 Using Procedural programming, Write a program to store the following figure (multiple of 1, 2, 3, 4 by its successors to three times). 4321 10862 1815123 2824204

15 Lets thinking now … Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 15 4321 10862 1815123 2824204 3210 0 1 2 3 ? How To

16 Next Lecture … Mahmoud Rafeek Alfarra Downloaded from Downloaded from http://staff.cst.ps/mfarra 16 Continue Array’s Application


Download ppt "Computer Programming 2 Lecture 1: Advanced Array Data Structure Using Methods Prepared & Presented by: Mahmoud Rafeek Alfarra MINISTRY OF EDUCATION & HIGHER."

Similar presentations


Ads by Google