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
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
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
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
What is array? Presented & Prepared by: Mahmoud R. Alfarra
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
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
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
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
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
Example1: Creating and Initializing an Array Presented & Prepared by: Mahmoud R. Alfarra 11
Example2: Creating and Initializing an Array Using an array initializer Presented & Prepared by: Mahmoud R. Alfarra 12
Example3: Creating and Initializing an Array Using an array initializer Presented & Prepared by: Mahmoud R. Alfarra 13
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
Example5: Computing the sum of the elements of an array. Presented & Prepared by: Mahmoud R. Alfarra 15
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
Example: Enhanced for Statement HW 12.1 Re-write all the previous examples using the enhanced for Presented & Prepared by: Mahmoud R. Alfarra 17
Example: Searching in array Presented & Prepared by: Mahmoud R. Alfarra
Example: Searching in array Presented & Prepared by: Mahmoud R. Alfarra
Example: Sorting an integer array HW 12.2 Write a program to sort a string array Presented & Prepared by: Mahmoud R. Alfarra 20
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
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
Emank X Mezank قال رسول الله صلى الله عليه وسلم: ( من دعا إلى هُدى كان له من الأجر مثل أجور من تَبِعَه لا ينقص ذلك من أجورهم شيئا) Presented & Prepared by: Mahmoud R. Alfarra
Next… Practices Presented & Prepared by: Mahmoud R. Alfarra