Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction To Programming Information Technology , 1’st Semester

Similar presentations


Presentation on theme: "Introduction To Programming Information Technology , 1’st Semester"— Presentation transcript:

1 Introduction To Programming Information Technology , 1’st Semester
MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Using Java Introduction To Programming Information Technology , 1’st Semester Lecture 12,13 Arrays Teacher: Mahmoud Rafeek Alfarra

2 Outline Data types categories What is array and why?
Declaring and Creating Arrays Examples Using Arrays Enhanced for Statement Example: Searching in array Example: Sorting array Multidimensional Arrays Emank X Mezank Presented & Prepared by: Mahmoud R. Alfarra

3 Data types categories In Programming, Types are divided into two categories Primitive types. int float char Reference types Object of any user-identified class Arrays Presented & Prepared by: Mahmoud R. Alfarra

4 What is array? Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities they remain the same length once they are created(static data structure). Presented & Prepared by: Mahmoud R. Alfarra

5 What is array? Presented & Prepared by: Mahmoud R. Alfarra

6 Why array? An array is a group of variables (called elements or components) containing values that all have the same type. So, we can use it to store any related values and perform the operations on it as sorting, searching, printing , … Presented & Prepared by: Mahmoud R. Alfarra 6

7 Refers to elements in array
A program refers to any one of these elements with an array-access expression that written as follow: Array [ i ] = k The name of array The value which assigns to the element The index of elements Begins from 0 to length-1 The first element in every array has index zero and is sometimes called the zeroth element. Presented & Prepared by: Mahmoud R. Alfarra

8 Declaring and Creating Arrays
Arrays occupy space in memory. The programmer specifies the type of the elements and uses operator new to allocate dynamically the number of elements required by each array. Arrays are allocated with new because arrays are objects and all objects must be created with new. type[] array_name = new type[ x ]; Type of elements which will be allocated in the array Number of elements which will be allocated in the array Presented & Prepared by: Mahmoud R. Alfarra

9 Declaring and Creating Arrays
int[] grades = new int[ 70 ]; String[] employees = new String[ 10 ]; float[] salary = new float[ 30 ]; Using a value of type long as an array index results in a compilation error. An index must be an int value or a value of a type that can be Presented & Prepared by: Mahmoud R. Alfarra

10 Be care In an array declaration, specifying the number of elements in the square brackets of the declaration (e.g., int c[ 12 ];) is a syntax error. Declaring multiple array variables in a single declaration can lead to subtle errors. Consider the declaration int[] a, b, c;. If a, b and c should be declared as array variables, then this declaration is correct placing square brackets directly following the type indicates that all the identifiers in the declaration are array variables. However, if only a is intended to be an array variable, and b and c are intended to be individual int variables, then this declaration is incorrect the declaration int a[], b, c; would achieve the desired result. Presented & Prepared by: Mahmoud R. Alfarra 10

11 Example1: Creating and Initializing an Array
Presented & Prepared by: Mahmoud R. Alfarra 11

12 Example2: Creating and Initializing an Array
Using an array initializer Presented & Prepared by: Mahmoud R. Alfarra 12

13 Example3: Creating and Initializing an Array
Using an array initializer Presented & Prepared by: Mahmoud R. Alfarra 13

14 Example4: Calculating a Value to Store in Each Array Element
Assigning a value to a constant after the variable has been initialized is a compilation error. Presented & Prepared by: Mahmoud R. Alfarra 14

15 Example5: Computing the sum of the elements of an array.
Presented & Prepared by: Mahmoud R. Alfarra 15

16 for ( type arg : arrayName ) expression
Enhanced for Statement Enhanced for iterates through the elements of an array or a collection without using a counter. The syntax of an enhanced for statement is: for ( type arg :  arrayName )           expression Presented & Prepared by: Mahmoud R. Alfarra 16

17 Example: Enhanced for Statement
HW 12.1 Re-write all the previous examples using the enhanced for Presented & Prepared by: Mahmoud R. Alfarra 17

18 Example: Searching in array
Presented & Prepared by: Mahmoud R. Alfarra

19 Example: Searching in array
Presented & Prepared by: Mahmoud R. Alfarra

20 Example: Sorting an integer array
HW 12.2 Write a program to sort a string array Presented & Prepared by: Mahmoud R. Alfarra 20

21 Multidimensional Arrays
Multidimensional arrays with two dimensions are often used to represent tables of values consisting of information arranged in rows and columns. Presented & Prepared by: Mahmoud R. Alfarra 21

22 Creating Two-Dimensional Arrays
Multidimensional arrays with two dimensions are often used to represent tables of values consisting of information arranged in rows and columns. Type array_name[ ][ ] = new type[ a ][ b ]; int array[ ][ ] = new int[ 3 ][ 4 ]; Presented & Prepared by: Mahmoud R. Alfarra 22

23 Emank X Mezank قال رسول الله صلى الله عليه وسلم: ( من دعا إلى هُدى كان له من الأجر مثل أجور من تَبِعَه لا ينقص ذلك من أجورهم شيئا) Presented & Prepared by: Mahmoud R. Alfarra

24 Next… Practices Presented & Prepared by: Mahmoud R. Alfarra


Download ppt "Introduction To Programming Information Technology , 1’st Semester"

Similar presentations


Ads by Google