Arrays Chapter 7.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

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]
 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.
 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.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
 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.
 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.
The University of Texas – Pan American
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.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
 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.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
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.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
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 (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Lecture 7: Arrays Michael Hsu CSULA 3 Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Array, Strings and Vectors
An Example to Show Command-Line Arguments and Wrapper Class
JavaScript: Functions.
The University of Texas – Pan American
Arrays, For loop While loop Do while loop
Java How to Program, Late Objects Version, 10/e
Chapter 6 - Arrays Outline 6.1 Introduction 6.2 Arrays
7 Arrays.
7 Arrays.
Review of Arrays and Pointers
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
JavaScript Arrays.
Multidimensional Arrays
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Arrays.
Data Structures (CS212D) Week # 2: Arrays.
Arrays Week 2.
Building Java Programs
7 Arrays.
10 JavaScript: Arrays.
C++ Array 1.
Arrays.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

Arrays Chapter 7

"All students to receive arrays!" reports Dr. Austin. Declaring arrays Passing arrays as parameters Stepping through arrays Inspecting arrays scores : 85 79 92 57 68 80 . . . 0 1 2 3 4 5 98 99

Declaring and Creating Arrays Recall that an array is a collection of elements all of the same type Array objects in Java must be created with the key word new Syntax int c[] = new int [12]; Results: 12 integer locations are allocated They are initialized to 0 (null for reference variables, false for boolean) Note the difference in the declaration - the number of elements is not specified in the first set of square brackets.

Using Arrays View example Figure 7.2 Note Declaration of the array Allocation of memory with new Initialization of array elements Use of array attribute (length) Printing of array contents

The Array Initializer A comma separated list of expressions The initializer list Enclosed in braces View the Example Note again, how the array knows its own length for (int count = 0 ; count < array.length ; count++) . . .

Processing Elements of An Array On Average People Are Mean Finding and using the arithmetic mean Declare the array, create Read in values, count how many Declare a summation variable, initialize Sum the array, calculate average Sort the array Print results, accessing the array See sample program

Histogram Program Use the value in the array to View Figure 7.6, Print the value Print a string of asterisks for visualization of size of the value View Figure 7.6,

Using Array Elements as Counters Recall program which tested random number generator (Figure 6.8) Used 6 different variables Used switch statement to increment At the time we noted the inefficiency Consider a new version of the program Uses array elements to total the number of rolls of each number 1 – 6 No switch statement needed View Figure 7.7

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 View example, Figure 7.12

References and Reference Parameters 17 In Java, primitive-type variables are always passed by value Incoming data only Objects are not passed to methods References to objects are passed The reference is passed by value With a reference to the object the method can manipulate the object directly Object Address of object

References and Reference Parameters Passing arrays by reference Improves performance Saves time Initialization of large array not required Saves memory Use memory already occupied by array No new memory allocation required View example program, Figure 7.13

Using Arrays Sorting Arrays Searching Arrays Recall previous example Linear search (see example) Step through array until desired value located Binary search Array must be sorted Look in middle, then above or below Look in middle of sub-section then above or below Etc.

Multidimensional Arrays Java does not support multidimensional arrays directly Does allow declaration of arrays whose elements are arrays! int b[][]; b = new int[ 2 ][ ]; // allocate rows b[ 0 ] = new int[ 5 ]; // allocate columns for row 0 b[ 1 ] = new int[ 3 ]; // allocate columns for row 1

Fig. 7.16 | Two-dimensional array with three rows and four columns. Multidimensional Arrays Fig. 7.16 | Two-dimensional array with three rows and four columns.

Creating Two-dimensional Arrays Can be created dynamically 3-by-4 array int b[][]; b = new int[ 3 ][ 4 ]; Rows can have different number of columns int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row View Example Figure 7.17

Variable-Length Argument Lists New feature in J2SE 5.0 Unspecified number of arguments Use ellipsis (…) in method’s parameter list Can occur only once in parameter list Must be placed at the end of parameter list Array whose elements are all of the same type View example, Fig. 7.20

Using Command-Line Arguments Pass arguments from the command line String args[] Appear after the class name in the java command java MyClass a b Number of arguments passed in from command line args.length First command-line argument args[ 0 ] View Example Figure 7.21