 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Outline IS400: Development of Business Applications on the Internet Fall 2004 Instructor: Dr. Boris Jukic JavaScript: Arrays.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 14 – Student Grades Application: Introducing.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
5 5 Arrays. OBJECTIVES In this lecture we will learn:  Case switch  To use the array data structure to represent a set of related data items.  To declare.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Introduction to Computers and Programming Lecture 16: Arrays (cont) Professor: Evan Korth New York University.
Arrays Chapter 6.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2005 Pearson Education, Inc. All rights reserved Arrays.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
 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.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 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.
 2006 Pearson Education, Inc. All rights reserved Arrays.
CHAPTER 07 Arrays and Vectors (part I). OBJECTIVES 2 In this part you will learn:  To use the array data structure to represent a set of related data.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
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.
 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.
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.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
 Pearson Education, Inc. All rights reserved Passing Arrays to Methods To pass array argument to a method – Specify array name without.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
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.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
 2008 Pearson Education, Inc. All rights reserved Arrays and Vectors.
 2006 Pearson Education, Inc. All rights reserved Arrays and Vectors.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
© 2016 Pearson Education, Ltd. All rights reserved.
Java How to Program, Late Objects Version, 10/e
7 Arrays.
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
7 Arrays.
Presentation transcript:

 2005 Pearson Education, Inc. All rights reserved. 1 Arrays

 2005 Pearson Education, Inc. All rights reserved. 2 Introduction Arrays – Data structures – Related data items of same type – Remain same size once created Fixed-length entries

 2005 Pearson Education, Inc. All rights reserved. 3 Fig. | A 12-element array.

 2005 Pearson Education, Inc. All rights reserved. 4 Arrays (Cont.) Index – Also called subscript – Position number in square brackets – Must be positive integer or integer expression – First element has index zero a = 5; b = 6; c[ a + b ] += 2; Adds 2 to c[ 11 ]

 2005 Pearson Education, Inc. All rights reserved. 5 Arrays (Cont.) Examine array c – c is the array name – c.length accesses array c ’s length – c has 12 elements ( c[0], c[1], … c[11] ) The value of c[0] is –45

 2005 Pearson Education, Inc. All rights reserved. 6 Declaring and Creating Arrays Declaring and Creating arrays – Arrays are objects that occupy memory – Created dynamically with keyword new int c[] = new int[ 12 ]; Equivalent to int c[]; // declare array variable c = new int[ 12 ]; // create array

 2005 Pearson Education, Inc. All rights reserved. 7 Common Programming Error 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.

 2005 Pearson Education, Inc. All rights reserved. 8 Common Programming 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.

 2005 Pearson Education, Inc. All rights reserved. 9 Examples Using Arrays Creating and initializing an array – Declare array – Create array – Initialize array elements

 2005 Pearson Education, Inc. All rights reserved. 10

 2005 Pearson Education, Inc. All rights reserved. 11 Using Arrays (Cont.) Using an array initializer – Use initializer list Items enclosed in braces ( {} ) Items in list separated by commas int n[] ={ 10, 20, 30, 40,50 }; – Creates a five-element array – Index values of 0, 1, 2, 3, 4 – Do not need keyword new

 2005 Pearson Education, Inc. All rights reserved. 12

 2005 Pearson Education, Inc. All rights reserved. 13 Examples Using Arrays (Cont.) Summing the elements of an array – Array elements can represent a series of values We can sum these values

 2005 Pearson Education, Inc. All rights reserved. 14

 2005 Pearson Education, Inc. All rights reserved. 15 Error-Prevention Tip When writing code to loop through an array, ensure that the array index is always greater than or equal to 0 and less than the length of the array. The loop-continuation condition should prevent the accessing of elements outside this range.

 2005 Pearson Education, Inc. All rights reserved. 16 Enhanced for Statement Enhanced for statement – New feature of J2SE 5.0 – Allows iterates through elements of an array or a collection without using a counter – Syntax for ( parameter : arrayName ) statement

 2005 Pearson Education, Inc. All rights reserved. 17 Passing Arrays to Methods To pass array argument to a method – Specify array name without brackets Array x is declared as int X = new int[ 24 ]; The method call modifyArray( X ); Passes array X to method modifyArray

 2005 Pearson Education, Inc. All rights reserved. 18

 2005 Pearson Education, Inc. All rights reserved. 19

 2005 Pearson Education, Inc. All rights reserved. 20 Passing Arrays to Methods (Cont.) Notes on passing arguments to methods – Two ways to pass arguments to methods Pass-by-value – Copy of argument’s value is passed to called method – In Java, every primitive is pass-by-value Pass-by-reference – Caller gives called method direct access to caller’s data – Called method can manipulate this data – In Java, every object is pass-by-reference In Java, arrays are objects Therefore, arrays are passed to methods by reference

 2005 Pearson Education, Inc. All rights reserved. 21 Multidimensional Arrays Multidimensional arrays – Tables with rows and columns Two-dimensional array m-by-n array

 2005 Pearson Education, Inc. All rights reserved. 22 Fig. | Two-dimensional array with three rows and three columns.

 2005 Pearson Education, Inc. All rights reserved. 23 Two-dimensional Arrays (Cont.) Declaring two-dimensional array b[2][2] int b[][] = { { 1, 2 }, { 3, 4 } }; 1 and 2 initialize b[0][0] and b[0][1] 3 and 4 initialize b[1][0] and b[1][1]

 2005 Pearson Education, Inc. All rights reserved. 24

 2005 Pearson Education, Inc. All rights reserved. 25