Chapter 10 Introduction to Arrays

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Chapter 8 Improving the User Interface
Chapter 6 Introduction to Defining Classes
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
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.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Chapter 9 Introduction to Arrays
 Pearson Education, Inc. All rights reserved Arrays.
CS0007: Introduction to Computer Programming Introduction to Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
 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.
A First Book of ANSI C Fourth Edition
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.
Chapter 8 Arrays and Strings
Arrays Mr. Smith AP Computer Science A.
Chapter 11 Arrays Continued
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.
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.
Chapter 8: Arrays.
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.
Java SE 8 for Programmers, Third Edition
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Textbook: Data Structures and the Java Collections Framework 3rd Edition by William Collins William Collins.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 5 Introduction to Defining Classes
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
1 Sections 3.1 – 3.2a Basic Syntax and Semantics Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Lesson 8: Introduction To Arrays
Sections 10.1 – 10.4 Introduction to Arrays
Computer Programming BCT 1113
© 2016 Pearson Education, Ltd. All rights reserved.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Section 11.1 Class Variables and Methods
Lesson 9: Introduction To Arrays (Updated for Java 1
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays.
Presentation transcript:

Chapter 10 Introduction to Arrays Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne

Objectives Write programs that handle collections of similar items. Declare array variables and instantiate array objects. Manipulate arrays with loops, including the enhanced for loop. Write methods to manipulate arrays. Create parallel and two-dimensional arrays. 2 2

Vocabulary array element enhanced for loop index initializer list logical size parallel arrays physical size procedural decomposition range-bound error structure chart subscript 3 3

Introduction An array is a data structure that consists of an ordered collection of similar items. An array has a single name. The items in an array are referred to in terms of their position in the array. Arrays are used to manipulate multiple values. 4 4

Conceptual Overview The items in an array are called elements. All of the elements need to be of the same type. The type can be any primitive or reference type. The length of an array is measured by the number of elements. The first element is element[0], the second is element[1], etc. An item’s position with an array is its index or subscript. 5 5

Conceptual Overview (continued) Three arrays, each containing five elements 6 6

Simple Array Manipulations The mechanics of manipulating arrays are fairly straightforward. First, declare and instantiate the array. <array name>[<index>] Index must be between 0 and the length minus 1. The subscript operator ([ ]) has the same precedence as the method selector (.). 7 7

Simple Array Manipulations (continued) The JVM checks the values of subscripts before using them. Throws an exception if they are out of bounds (less than 0 or greater than array length minus 1). The detection of a range-bound error is similar to the JVM’s behavior when a program attempts to divide by 0. An array’s length is stored in the public instance variable length. 8 8

Looping Through Arrays Traversal: a loop that iterates through an array one element at a time. Count the Occurrences: 9 9

Looping Through Arrays (continued) Other examples: Sum the elements Determine presence of absence of a number Determine first location To work with arrays of any size, use the length instance variable in the loop. 10 10

Declaring Arrays Example: declaring an array of 500 integers. Arrays are objects and must be instantiated before using. 11 11

Declaring Arrays (continued) Array variables are null before they are assigned array objects. Failure to assign an array object can result in a null pointer exception. Two variables can refer to the same array. To have two variables refer to two separate arrays that have the same values, copy all of the elements from one array to the other. 12 12

Declaring Arrays (continued) Two variables can refer to the same array object 13 13

Declaring Arrays (continued) Because arrays are objects, Java’s garbage collector sweeps them away when they are no longer referenced. Arrays can be declared, instantiated and initialized in one step. The list of numbers between the braces is called an initializer list. 14 14

Declaring Arrays (continued) Arrays can be formed from any collections of similar items. Booleans, doubles, characters, strings, and students. Once an array is instantiated, its size cannot be changed, so make sure the array is large enough from the outset. 15 15

Working with Arrays That Are Not Full When an array is instantiated, the computer fills its cells with default values. Then the application replaces the values with new ones as needed. An application might not use all of the cells available in an array. Physical size: the number of cells in an array. Logical size: the number of cells being used. 16 16

Working with Arrays That Are Not Full (continued) Processing Elements in an Array That Is Not Full: When the array is not full, one must replace the physical length with its logical size. Adding Elements to an Array: Place the element to be added directly after the last available item. Check to see if there is a cell, and change the logical size. 17 17

Working with Arrays That Are Not Full (continued) Removing Elements from an Array: Decrement the logical size, which prevents the application from accessing the garbage elements beyond that point. Arrays and Text Files: Text files can be used for output and input. 18 18

Parallel Arrays Parallel arrays: using two arrays in which corresponding elements are related. Example: An array includes strings of people’s names. A second array includes integers of the same people’s ages. 19 19

Using the Enhanced for Loop An enhanced for loop visits each element in an array from the first position to the last position. On each pass, the element at the current position is assigned a temporary variable. The temporary variable has to be compatible with element type of the array. Allows programmer to skip the use of index variables and other tests. 20 20

Using the Enhanced for Loop (continued) A break statement can be used to terminate an enhanced for loop early. Enhanced for loops are simpler and less error-prone than for loops with an index. 21 21

Using the Enhanced for Loop (continued) The enhanced for loop cannot be used to: Reverse through an array. Assign elements to positions in an array. Track the index position of the current element. Access any element other than the current element on each pass. Also, an enhanced for loop shouldn’t be used for an array that’s not filled. 22 22

Arrays and Methods When an object is used as a parameter to a method, what actually gets passed is a reference to the object. Not the object itself. The actual and formal parameters refer to the same object. Changes made to the object’s state are in effect after the method terminates. 23 23

Arrays and Methods (continued) Passing a reference to an object as a parameter 24 24

Arrays and Methods (continued) Arrays are objects, so the same rules apply. When an array is passed as a parameter to a method, the method manipulates the array itself. Changes made to the array in the method are in effect after the method is executed. Passing an array to a method leads to trouble if the method mishandles the array. A method can instantiate a new object or array and return it using the return statement. 25 25

Arrays and Methods (continued) Example: copy an array. 26 26

Arrays of Objects Arrays can hold references to objects of any type. When an array of objects is instantiated, each cell is null by default until reset to a new object. 27 27

Graphics and GUIs: Changing the View of Student Test Scores Organizing the code between the model and the view splits the code between: Managing the interface. Manipulating database. A GUI interface to view a database needs buttons to support navigating between records. Also to add or modify records. 28 28

Graphics and GUIs: Changing the View of Student Test Scores (continued) GUI for the student test scores program 29 29

Graphics and GUIs: Changing the View of Student Test Scores (continued) Description of buttons: 30 30

Design, Testing, and Debugging Hints To set up an array: Declare an array variable. Instantiate an array object and assign it to the array variable. Initialize the cells in the array with data, as appropriate. Try to estimate the number of cells needed for an array when creating it. 31 31

Design, Testing, and Debugging Hints (continued) Array variables are null until assigned objects. The index of an array cell ranges from 0 to the length of the array minus 1. To access the last cell, use <array>.length-1. Avoid having more than one array variable refer to the same array object. When an array is not full, track the current number of elements. 32 32

Summary In this chapter, you learned: Arrays are collections of similar items or elements. The items in arrays are ordered by position. Arrays are useful when a program needs to manipulate many similar items, such as a group of students or a number of test scores. 33 33

Summary (continued) Arrays are objects. Thus, they must be instantiated and they can be referred to by more than one variable. An array can be passed to a method as a parameter and returned as a value. Parallel arrays are useful for organizing information with corresponding elements. 34 34

Summary (continued) Two-dimensional arrays store values in a row-and-column arrangement similar to a table. The enhanced for loop is a simplified version of a loop for visiting each element of an array from the first position to the last position. 35 35