Unit 12 JavaScript Arrays Instructor: Brent Presley.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 10 Introduction to Arrays
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Arrays. A group of data with same type stored under one variable. It is assumed that elements in that group are ordered in series. In C# language arrays.
25-Jun-15 JavaScript Language Fundamentals II. 2 Exception handling, I Exception handling in JavaScript is almost the same as in Java throw expression.
Hash Tables1 Part E Hash Tables  
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
C++ for Engineers and Scientists Third Edition
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
© The McGraw-Hill Companies, 2006 Chapter 7 Implementing classes.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Java Unit 9: Arrays Declaring and Processing Arrays.
MySQL in PHP – Page 1 of 17CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: MySQL in PHP Reading: Williams &
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Programming Languages -1 (Introduction to C) arrays Instructor: M.Fatih AMASYALI
 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.
Lists in Python.
 2006 Pearson Education, Inc. All rights reserved Arrays.
Characters The data type char represents a single character in Java. –Character values are written as a symbol: ‘a’, ‘)’, ‘%’, ‘A’, etc. –A char value.
Fall 2001(c)opyright Brent M. Dingle 2001 Arrays Brent M. Dingle Texas A&M University Chapter 9 – Sections 1 and 2 (and some from Mastering Turbo Pascal.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Searching and Sorting Chapter Sorting Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Arrays and 2D Arrays.  A Variable Array stores a set of variables that each have the same name and are all of the same type.  Member/Element – variable.
© 2012 EMC Publishing, LLC Slide 1 Chapter 8 Arrays  Can store many of the same type of data together.  Allows a collection of related values to be stored.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
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.
Chapter 6: Arrays: Lists and Tables
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Project 1: Using Arrays and Manipulating Strings Essentials for Design JavaScript Level Two Michael Brooks.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
1 CSCD 326 Data Structures I Hashing. 2 Hashing Background Goal: provide a constant time complexity method of searching for stored data The best traditional.
Composition When one class contains an instance variable whose type is another class, this is called composition. Instead of inheritance, which is based.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Unit 10-JavaScript Functions Instructor: Brent Presley.
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. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays and Lists. What is an Array? Arrays are linear data structures whose elements are referenced with subscripts. Just about all programming languages.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Chapter 11 - JavaScript: Arrays
Containers and Lists CIS 40 – Introduction to Programming in Python
7 Arrays.
PHP.
Can store many of the same kind of data together
Object Oriented Programming in java
JavaScript Arrays.
7 Arrays.
Data Structures & Algorithms
Presentation transcript:

Unit 12 JavaScript Arrays Instructor: Brent Presley

REFERENCES -associative-arrays-demystified/ -associative-arrays-demystified/

CONCEPTS arrays are many(sequential) memory locations that are all referenced by the same name eachitem in the array is an element and is referenced by a 0 based counter since variables do not have types in JavaScript, JavaScript arrya elements can be of varying types –some can be string, and others date, and some numeric in the same array array is a class in JavaScript JavaScript arrays are dynamic: their size can change as needed.

DECLARING ARRAYS var myArray = new Array( ); –Declares a new, empty array (size 0) –Code must add elements to the array var myArray = [ ]; –Alternate to above var myArray = new Array(15); –Declares a new array with 15 elements (0-14) –Code may change the size of the array

DECLARING ARRAYS var myArray = new Array(5.25, 10.11, 2.75, 9); –Declares a new array with 4 elements (0-3) –Initializes the elements of the array with the values designated –Code may change the size of the array var myArray = [5.25, 10.11, 2.75, 9]; –Alternate to above

ASSIGNING VALUES myArray[index] = newValue; –Index can be any value. If the value is beyond the end of the array, the array will automatically resize Quick way to add an element to the end of the array (and resize) myArray[myArray.length] = newValue; –length returns the number of elements in the array

DELETING ELEMENTS delete myArray[2] Removes the 3rd element of the array Size of the array is unaffected myArray[2] is now undefined

FOR LOOPS AND ARRAYS For loops and arrays go together like peanut butter and jelly. You rarely have arrays that aren’t processed with for loops for (var i=0; i<myArray.length; i++) for (var element in myArray)

NOTES REGARDING LOOPING Each time through the loop element has a different index value from the loop Big Note: Skips over elements that are undefined Big Note: if the array is indexed using integers (not associative array), the loop variable (element in this example) has type String (not integer)

PASSING ARRAYS TO FUNCTIONS doSomething(myArray); –Only the array name is included as an argument –Arrays are passed by reference. Only the address of the array is sent to the function. –Any changes made to the array by the function are also available outside the function

SORTING ARRAYS The Array class includes a sort method, arrayName.sort(); Because arrays can contain any kind of value, you also have to let the sort method know how to sort them. If you do not provide a function name (optional argument to sort ), sort sorts the array as strings

SORTING If you want to sort differently, you’ll have to define a comparator function that designates how two items would be sorted. –Include the function name as a parameter to sort arrayName.sort(howToCompare); –Note only the function name is included, no ( ) –The function must have two parameters These parameters are filled in by sort using two elements of the array –The function must: Return a negative number if parameter1 is less than parameter 2 Return 0 if the parameters are equal Return a positive number if parameter1 is greater than parameter 2

REVERSE SORTING If you want the array sorted in reverse order (descending), after sorting call the reverse method myArray.reverse();

CASE INSENSITIVE SORTING Sorting is normally done using the ASCII character sequence (little letters come after capital letters). If you want to sort ignoring case, you’ll have to define a comparator function.

EXAMPLE OF A SORT

SEARCHING ARRAYS Easiest way to search an array is to use the indexOf method built in to the Array class index = myArray.indexOf(findMe); Index will contain the index of findMe in myArray. If findMe does not exist, indexOf returns -1. indexOf includes an optional second parameter: start Designates where in the array to start searching Great for finding all occurrences in a string

LASTINDEXOF lastIndexOf method also exists—also includes an optional start parameter

BINARY SEARCH Used to search larger arrays Arrays must first be sorted Starts in the middle of the array Continually divides the array in half until the value has been found or the array can no longer be split. Extremely fast

ASSOCIATIVE ARRAYS Associative arrays use strings instead of numbers as indexes. PHP uses associative arrays when it receives values posted from a web form. An associative array basically duplicates the functionality of one record in a database, though it can be used for other purposes.

CREATE AN ASSOCIATIVE ARRAY Define the array: var studentData = []; Add elements to the array using strings as indexes studentData["firstName"] = "Fred"; studentData["lastName"] = "Flintstone"; studentData["program"] = "Prog/Analyst" studentData["GPA"] = 3.25; You can retrieve the data from the array in the same way, using the string indexes you used when defining it

CREATE ARRAY WITH INITIAL DATA Creating array with initial data var record = {"firstName":"Brent", "lastName":"Presley", "age":969 }; Actually creates an object but behaves like an associative array.

MULTIDIMENSIONAL ARRAY As in all languages, JavaScript allows you declare arrays with more than one dimension—more than one index JavaScript doesn’t support multidimensional arrays directly, but you can simulate them using arrays of arrays. Declaring var myArray = [ [1,2,3] [8,9,10]]; var twoDarray = new Array(2); twoDarray[0] = [1,2,3]; twoDarray[1] = [8,9,10];

ACCESSING INDIV ELEMENTS twoDarray[1][2] //Returns 10 Note multidimensional arrays also go well with for loops— nested for loops—one for each dimension