Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.

Slides:



Advertisements
Similar presentations
Chapter 10 Introduction to Arrays
Advertisements

Arrays part 3 Multidimensional arrays, odds & ends.
Arrays Chapter 6 Chapter 6.
Multi-Dimensional Arrays. A Table of Values Balances for Various Interest Rates Compounded Annually (Rounded to Whole Dollar Amounts) Year5.00%5.50%6.00%
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
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.
Chapter 5 - Arrays CSC 200 Matt Kayala 2/27/06. Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays Chapter 6 Array Basics Arrays in Classes and Methods
© The McGraw-Hill Companies, 2006 Chapter 16 Two-dimensional arrays.
11 Chapter 8 ARRAYS Continued. 22 MULTI-DIMENSIONAL ARRAYS A one-dimensional array is useful for storing/processing a list of values. For example: –The.
Java Unit 9: Arrays Declaring and Processing Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
Chapter 10Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 10 l Array Basics l Arrays in Classes and Methods l Programming.
1 Week 9 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
COS 260 DAY 16 Tony Gauvin.
Lecturer: Dr. AJ Bieszczad Chapter 6 COMP 150: Introduction to Object-Oriented Programming 6-1 l Array Basics l Arrays in Classes and Methods l Programming.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
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.
Two dimensional arrays in Java Computer Science 3 Gerb Objective: Use matrices in Java.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
CHAPTER: 12. Array is a collection of variables of the same data type that are referenced by a common name. An Array of 10 Elements of type double.
Chapter 11. Multidimensional Arrays and Vectors Multidimensional Arrays Vectors Computer Programming with JAVA.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Working with Arrays in MATLAB
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Computer Programming TCP1224 Chapter 11 Arrays. Objectives Using Arrays Declare and initialize a one-dimensional array Manipulate a one-dimensional array.
CPS120: Introduction to Computer Science Lecture 15 Arrays.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 6 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays ** ** multidimensional.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Multidimensional Arrays l Vectors Multidimensional Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
1 Arrays of Arrays Quick review … arrays Arrays of arrays ≡ multidimensional array Example: times table Representation in memory Ragged arrays Example:
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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.
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.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Arrays – two dimensional Chapter 14 This chapter explains how to do the following with a two dimensional array: Declare, use indices, obtain size, pass.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Arrays Chapter 7.
EKT472: Object Oriented Programming
Arrays Chapter 6 Array Basics Arrays in Classes and Methods
EKT150 : Computer Programming
Chapter 7 Part 2 Edited by JJ Shepherd
Presentation transcript:

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays in Classes and Methods l Programming with Arrays and Classes l Sorting Arrays l Multidimensional Arrays Arrays

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 2 Multidimensional Arrays l Arrays with more than one index »number of dimensions = number of indexes l Arrays with more than two dimensions are a simple extension of two-dimensional (2-D) arrays l A 2-D array corresponds to a table or grid »one dimension is the row »the other dimension is the column »cell: an intersection of a row and column »an array element corresponds to a cell in the table

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 3 Table as a 2-Dimensional Array l The table assumes a starting balance of $1000 l First dimension: row identifier - Year l Second dimension: column identifier - percentage l Cell contains balance for the year (row) and percentage (column) l Balance for year 4, rate 7.00% = $1311 Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 3

Table as a 2-D Array l Generalizing to two indexes: [row][column] l First dimension: row index l Second dimension: column index l Cell contains balance for the year/row and percentage/column l All indexes use zero-numbering »Balance[3][4] = cell in 4th row (year = 4) and 5th column (7.50%) »Balance[3][4] = $1311 (shown in yellow) Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 4 Row Index 3 (4th row) Column Index 4 (5th column)

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 5 2-D Arrays l a 2-D array is not a matrix or grid, as described in math l all arrays in Java use pointers to the array data; so 2D (or nD) arrays are actually, arrays or arrays (more later) »there are two indexes: the major and minor »the array "type" and data are stored in the minor to declare a 2-D array of int s named table[] »having 10 rows (major) and 6 columns (minor) int[][] table = new int[10][6];

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 6 Method to Calculate the Cell Values Each array element corresponds to the balance for a specific number of years and a specific interest rate (assuming a starting balance of $1000): balance(starting, years, rate) = (starting) x (1 + rate) years The repeated multiplication by (1 + rate) can be done in a for loop that repeats years times. balance() method in class InterestTable

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 7 Processing a 2-D Array: for Loops Nested 2-Deep Arrays and for loops are a natural fit To process all elements of an n -D array nest n for loops »each array index is controlled by it's own loop l For example: calculate and enter balances in the interest table »inner loop repeats 6 times (for six rates) for each outer loop iteration »the outer loop repeats 10 times (for 10 different values of years ) »so the inner repeats 10 x 6 = 60 times = # cells in table Excerpt from main() method of InterestTable

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 8 Multidimensional Array Parameters and Returned Values l Methods may have multi-D array parameters l Methods may return a multi-D array as the value returned l The situation is similar to 1-D arrays, but with more brackets Example: a 2-D int array as a method argument Notice how the number of columns is obtained Notice how the number of rows is obtained showTable() method from class InterestTable2

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 9 Multidimensional (nD) Arrays: "Arrays of Arrays" l Multidimensional arrays are actually 1D arrays of 1D arrays. Example: int[][] table = new int[3][4]; »table is a one-dimensional (1D) array of length 3 »each element in table is a 1D array with base type int. l Access a row by only using only one subscript (index): »table[0].length gives the length (4) of the first row in the array table[0] refers to the first row in the array, which is a one-dimensional array. Note: table.length (which is 3 in this case) is different than table[0].length (which is 4).

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 10 Ragged Arrays l Ragged arrays have rows of unequal length »each row has a different number of columns, or entries »essentially, each element in the first index points to another 1D array of an unknown length l Ragged arrays are basic to Java, but difficult (or impossible) to implement in other languages, such as C/C++ or Visual Basic Example: create a 2-D int array named b with 5 elements in the first row, 7 in the second row, and 4 in the third row: int[][] b; b = new int[3][]; b[0] = new int[5]; b[1] = new int[7]; b[2] = new int[4];

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 11 Programming Example : Employee Time Records The class TimeBook uses several arrays to keep track of employee time records: public class TimeBook { private int numberOfEmployees; private int[][] hours; private int[] weekHours; private int[] dayHours;... } hours[i][j] has the hours for employee j on day i weekHours[i] has the week's hours for employee i+1 dayHours[i] has the total hours worked by all employees on day i

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 12 Programming Example : Employee Time Records Nested Loops with Multidimensional Arrays the method computeWeekHours() uses nested for loops to compute the week's total hours for each employee. each time through the outer loop body, the inner loop adds all the numbers in one column of hours[] to get the value for one element in weekHours[]. for (employeeNumber = 1; employeeNumber <= numberOfEmployees; employeeNumber++) { // Process one employee sum = 0; for (dayNumber = 0; dayNumber < 5; dayNumber++) sum = sum + hours[dayNumber][employeeNumber – 1]; weekHours[employeeNumber – 1] = sum; } hours array weekHours array

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 13 Summary Part 1 l An array may be thought of as a collection of variables, all of the same type l An array is better thought of as a single object with a large composite value of all the elements of the array Arrays function similarly to objects created with new in a manner similar to objects discussed previously

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 14 Summary Part 2 l Array indexes use zero-numbering: »start at [0]; the index i refers to the (i+1)th element; »index of the last element is (length-of-the-array - 1) »index values outside the valid range cause an "array index out of bounds" exception (error) during run-time l A method can be passed an array in a parameter and return an array »the parameter and returned array are just the memory addresses l A "partially filled array" is one in which a limited number of array elements are used—a subset of the full array »a size variable must be used to track the used elements size

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 15 Summary Part 3 l A method can change the values of an array, only if the entire array (the array memory address) is passed. l Arrays can only store values of one base type (even if nD arrays); »to store more than one type of data, parallel arrays must be used »each arrays is of a different base type and the index keeps the arrays in synch l Arrays can have multiple dimensions, although… »the major dimension just points each of its elements to another single dimensional array and so one »the base type of the elements for the major dimension are memory addresses

Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 16 Summary Part 4 l Commonly, two-dimensional (2D) arrays can be viewed as a grid, matrix, or table with rows and columns: »one index is for the row, the other for the column »it is up to the programmer to clearly describe if the major index is the row or column (leaving the minor to be the other) l 3D, 4D, and nD arrays are just arrays or arrays »as far as Java is considered, all arrays are single dimension (1D) arrays »higher dimensions utilise the memory address as the base type for the first dimensions –the last dimension is always the base type of the arrray