Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
1 Arrays in JavaScript Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0]
Session 8 JavaScript/Jscript: Objects Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Session 5 JavaScript/JScript: Control Structures II Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
1 Pertemuan 14 Object Query Language (Lanjutan bagian 1) Matakuliah: M0174/OBJECT ORIENTED DATABASE Tahun: 2005 Versi: 1/0.
1 Pertemuan 04 Expression Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Arrays Chapter 6.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays 6.3Declaring Arrays 6.4Examples Using Arrays 6.5Passing.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays –Structures of related data items –Static entity (same size throughout program) A few types –Pointer-based.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Introduction to Programming with C++ Fourth Edition
Chapter 8 Arrays and Strings
JavaScript, Third Edition
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 6 - Arrays Outline 6.1Introduction 6.2Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
INE1020: Introduction to Internet Engineering 3: Web Page Authoring1 Lecture 6: Introduction to Javascript II r Javascript Objects Array String Date Math.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 6 Arrays Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Algorithm and Programming Array Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University of Indonesia 15 March 2009.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
C Lecture Notes 1 Arrays Lecture 6. C Lecture Notes 2 6.1Introduction Arrays –Structures of related data items –Static entity – same size throughout program.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
Chapter 17 – JavaScript/Jscript: Arrays
JavaScript: Functions.
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
Object Oriented Programming in java
JavaScript Arrays.
MSIS 655 Advanced Business Applications Programming
Data Structures (CS212D) Week # 2: Arrays.
Arrays Chapter 7.
Arrays Week 2.
7 Arrays.
JavaScript: Arrays.
10 JavaScript: Arrays.
Presentation transcript:

Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5

Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : menghasilkan web page dengan menerapkan struktur data Array dan konsep Object (C3)

7.1 Introduction 7.2 Arrays 7.3 Declaring and Allocating Arrays 7.4 Examples Using Arrays 7.5 References and Reference Parameters 7.6 Passing Arrays to Functions 7.7 Sorting Arrays 7.8 Searching Arrays: Linear 7.9 Multiple-Subscripted Arrays Outline Materi

7.1 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are “dynamic” entities –Can change size after being created

7.2 Arrays Array - group of memory locations that –All have the same name –Are normally of the same type (though not required) To refer to particular element in array, specify –The name of the array –The position number of the particular element in the array –Example (to identify the 5 th element in array c ): c[4] Position number in brackets called a subscript (or index) –If program uses an expression as a subscript, expression is evaluated first to determine the subscript continue..

7.2 Arrays First element in every array is the zeroth (0 th ) element –Therefore, element n will have a subscript value of ( n-1 ) Length of an Array determined by the expression arrayName.length Brackets –Used to enclose the subscript of an array –Have same precedence in JavaScript operations as parentheses continue..

7.2 Arrays Name of array (Note that all elements of this array have the same name, c ) Position number of the element within array c c[6] c[0] c[2] c[3] c[11] c[10] c[9] c[8] c[7] c[5] c[4] c[1]

7.3 Declaring and Allocating Arrays An array in JavaScript is an Array object Operator new creates an object as the program executes –Obtains enough memory to store an object of the type specified Process of creating objects also known as –Creating an instance or –Instantiating an object new –Dynamic memory allocation operator continue..

7.3 Declaring and Allocating Arrays Declaring and Allocating Arrays –To allocate 12 elements for integer array c var c = new Array( 12 ); –This can also be performed in 2 steps: 1.var c; 2.c = new Array( 12 ); –When arrays allocated, elements not initialized Reserving memory –Use a single declaration: var b = new Array( 100 ), x = new Array( 27 ); –Reserves 100 elements for array b, 27 elements for array x

7.4 Examples Using Arrays Arrays can be initialized with existing elements var n1 = new Array( 5 ); –Initializes array n1 with 5 elements Arrays can also be initialized with no elements –Grow dynamically to accommodate new elements var n2 = new Array(); –Initializes empty array n2 Element values can be printed using the regular writeln method document.writeln( “The value is “ + n2[2] ); Sample Program continue..

7.4 Examples Using Arrays The elements of an Array can be allocated and initialized in the array declaration This can be done in two ways –To initialize array n with five known elements : 1.var n = [ 10, 20, 30, 40, 50 ]; –Uses a comma-separated initializer list enclosed in square brackets 2.var n = new Array( 10, 20, 30, 40, 50 ); continue..

7.4 Examples Using Arrays To reserve a space in an Array for an unspecified value –Use a comma as a place holder in the initializer list var n = [ 10, 20,, 40, 50 ]; –Creates five element array with no value specified for n[2] –n[2] will appear undefined until a value for it is initialized Sample Program continue..

7.4 Examples Using Arrays for/in repetition structure –Enables a script to perform a task for each element in an array –Also known as iterating over the array elements Format: for ( var element in theArray ) total2 += theArray[ element ]; –Adds the value every element in array theArray to total2 –Structure ends after an iteration has been done for every element in theArray Sample Program1 Sample Program2

7.5 References and Reference Parameters Two ways to pass arguments to functions 1.Call-by-value / pass-by-value –When used to pass argument, copy of value is made and passed to called function –Takes up more space, uses more power, but more secure and eliminates many potential problems –Used in JavaScript for numbers or boolean values 2.Call-by-reference / pass-by-reference –When used to pass argument, location in memory / address of argument is passed to called function –Takes up less space, uses less power, but less secure and allows many potential problems to occur –Used in JavaScript for objects, including Array s

7.6 Passing Arrays to Functions To pass an array to a function –Specify the name of the array without brackets as a parameter –You do not need to separately pass the size of the array Individual numeric and boolean array elements are –Passed exactly as simple numeric and boolean variables: call-by-value –Simple single pieces of data are called scalars or scalar qualities –Are passed using subscripted name of the array element continue..

7.6 Passing Arrays to Functions For function to receive Array through a function call –Must specify parameter that will be used to refer to the array in the body of the function –JavaScript does not provide a special syntax for this purpose arrayName.join( x ); –Creates string containing all elements in arrayName –Takes argument – string containing separator – used to separate elements of the array in the string when returned –If argument left empty – empty string used as separator Sample Program

7.7 Sorting Arrays Sorting data is one of most important computing scripts –Virtually every organization must sort data in some amount Array object in Javascript has built-in function sort –No arguments Uses string comparisons to determine sorting order –With optional arguments Takes the name of a function called the comparator function Comparator function takes two arguments and returns –A negative value if the first argument is less than the second –Zero if the arguments are equal –A positive value if the first argument is greater than the second Sample Program

7.8 Searching Arrays: Linear When working with large amounts of data –May be necessary to determine whether an array contains a value that matches a certain key value –Searching – process of locating particular element value in an array Linear searching –Compares each element in an array with a search key –Goes in order of elements in array If array unsorted, just as likely value will be found in first element as the last element –Works well for small arrays or unsorted arrays –Inefficient for large arrays Sample Program

7.9 Multiple-Subscripted Arrays Multiple-Subscripted Arrays with two subscripts –Often represent tables of values consisting of info arranged in rows and columns Double-subscripted array (or two-dimensional arrays) –Require two subscripts to identify a particular element First subscript identifies element’s row Second subscript identifies element’s column m-by-n array –An array with m rows and n columns continue..

7.9 Multiple-Subscripted Arrays Double-scripted array with three rows and four columns continue.. a[0][0]a[0][1] a[1][0]a[1][1] a[2][0]a a[0][2] a[1][2] a[0][3] a[1][3] a[2][2]a[2][3][2][1] Column 3Column 0Column 2Column 1 Row 0 Row 1 Row 2 Column subscript Row subscript Array name

7.9 Multiple-Subscripted Arrays Initialization –Declared like a single-scripted array –Double scripted array b with two rows and two columns could be declared and initialized with var b = [ [ 1, 2 ], [ 3, 4, 5 ] ]; –Compiler determines number of elements in row/column By counting number of initializer values in sub-initializer list for that row/column Can have a different number of columns in each row for and for/in loops used to traverse the arrays –Manipulate every element of the array Sample Program continue..

7.9 Multiple-Subscripted Arrays Different array manipulations using for and for/in : for( int col = 0; col < a[2].length; ++col ) a[ 2 ][ col ] = 0; identical to for ( var col in a[ 2 ] ) a[ 2 ][ col ] = 0 Both statements set all elements in third row of array a to zero continue..

7.9 Multiple-Subscripted Arrays Different array manipulations using for and for/in var total = 0; for (var row = 0; row < a.length; ++row ) for (var col = 0; col < a[ row ].length; ++col ) total += a[ row ][ col ]; identical to var total = 0; for (var row in a ) for (var col in a[ row ] ) total += a[ row ][ col ]; –Both statements total the elements of the array, one row at a time

End of Session 7