Chapter 8: Arrays.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Arrays.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
Programming Logic and Design Sixth Edition
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Understanding Arrays and Pointers Object-Oriented Programming Using C++ Second Edition 3.
Chapter 6 C Arrays Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc. Arrays are data structures.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Chapter 8 Arrays and Strings
JavaScript, Third Edition
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Programming Logic and Design, Third Edition Comprehensive
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 9: Advanced Array Concepts
Chapter 8 Arrays and Strings
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
 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.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
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.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 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.
Chapter 7: Characters, Strings, and the StringBuilder.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 8 Arrays.
Chapter 13: Structures. In this chapter you will learn about: – Single structures – Arrays of structures – Structures as function arguments – Linked lists.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Visual C# 2005 Using Arrays. Visual C# Objectives Declare an array and assign values to array elements Initialize an array Use subscripts to access.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
Chapter VII: Arrays.
Chapter 6: Using Arrays.
An Introduction to Programming with C++ Sixth Edition
BASIC ELEMENTS OF A COMPUTER PROGRAM
Computer Programming BCT 1113
Chapter 3: Using Methods, Classes, and Objects
Selenium WebDriver Web Test Tool Training
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.
Object Oriented Programming in java
CIS16 Application Development and Programming using Visual Basic.net
Programming Logic and Design Fifth Edition, Comprehensive
Presentation transcript:

Chapter 8: Arrays

Objectives Declare arrays Initialize an array Use variable subscripts with an array Declare and use arrays of objects Search an array and use parallel arrays Pass arrays to and return arrays from methods Java Programming, Seventh Edition

Declaring Arrays Array Declare an array variable A named list of data items All data items have the same type Declare an array variable The same way as declaring any simple variable Insert a pair of square brackets after the type double[] salesFigure; int[] idNums; Java Programming, Seventh Edition

Declaring Arrays (cont’d.) Still need to reserve memory space sale = new double[20]; double[] sale = new double[20]; Subscript An integer contained within square brackets Indicates one of the array’s variables or elements A subscript that is too small or too large for an array is out of bounds An error message is generated Java Programming, Seventh Edition

Declaring Arrays (cont’d.) An array’s elements are numbered beginning with 0 You can legally use any subscript from 0 through 19 when working with an array that has 20 elements When working with any individual array element, treat it no differently than a single variable of the same type Example: sale[0] = 2100.00; Java Programming, Seventh Edition

Declaring Arrays (cont’d.) Figure 8-1 The first few and last few elements of an array of 20 salesFigures items in memory Java Programming, Seventh Edition

Initializing an Array A variable with a reference type, such as an array, holds a memory address where a value is stored Array names: Represent computer memory addresses Contain references When you declare an array name: No computer memory address is assigned The array has the special value null Unicode value ‘\u0000’ Java Programming, Seventh Edition

Initializing an Array (cont’d.) Use the keyword new to define an array The array name acquires the actual memory address value int[] someNums = new int[10]; Each element of someNums has a value of 0 char array elements Assigned ‘\u0000’ boolean array elements Automatically assigned the value false Strings and arrays of objects Assigned null by default Java Programming, Seventh Edition

Initializing an Array (cont’d.) Assign nondefault values to array elements upon creation int[] tenMult = {10, 20, 30, 40, 50, 60}; An initialization list initializes an array Values are separated by commas and enclosed within curly braces Populating an array Providing values for all the elements in an array Java Programming, Seventh Edition

Using Variable Subscripts with an Array Scalar A primitive variable Power of arrays Use subscripts that are variables rather than constant subscripts Use a loop to perform array operations for (sub = 0; sub < 5; ++sub) scoreArray[sub] += 3; Java Programming, Seventh Edition

Using Variable Subscripts with an Array (cont’d.) When an application contains an array: Use every element of the array in some task Perform loops that vary the loop control variable Start at 0 End at one less than the size of the array It is convenient to declare a symbolic constant equal to the size of the array final int NUMBER_OF_SCORES = 5; Java Programming, Seventh Edition

Using Variable Subscripts with an Array (cont’d.) Field An instance variable Automatically assigned a value for every array created length field Contains the number of elements in the array for(sub = 0; sub < scoreArray.length; ++sub) scoreArray[sub] += 3; length is a property of the object Is a field Cannot be used as an array method Java Programming, Seventh Edition

Using Variable Subscripts with an Array (cont’d.) Enhanced for loop Allows you to cycle through an array without specifying starting and ending points for the loop control variable for(int val : scoreArray) System.out.println(val); Java Programming, Seventh Edition

Using Part of an Array In cases when you do not want to use every value in an array Java Programming, Seventh Edition Figure 8-4 The FlexibleQuizAverage application

Declaring and Using Arrays of Objects Create an array of Employee objects Employee[] emp = new Employee[7]; Must call seven individual constructors final double PAYRATE = 6.35; for(int x = 0; x < NUM_EMPLOYEES; ++x) emp[x] = new Employee(101 + x, PAYRATE); Java Programming, Seventh Edition

Using the Enhanced for Loop with Objects Use the enhanced for loop to cycle through an array of objects Eliminates the need to use a limiting value Eliminates the need for a subscript following each element for(Employee worker : emp) System.out.println(worker.getEmpNum() + " " + worker.getSalary(); Java Programming, Seventh Edition

Manipulating Arrays of Strings Create an array of Strings String[] deptNames = {"Accounting", "Human Resources", "Sales"}; for(int a = 0; a < deptNames.length; ++a) System.out.println(deptNames[a]); Java Programming, Seventh Edition

Searching an Array and Using Parallel Arrays Determine whether a variable holds one of many valid values Use a series of if statements Compare the variable to a series of valid values Java Programming, Seventh Edition

Searching an Array and Using Parallel Arrays (cont’d.) Compare the variable to a list of values in an array for(int x = 0; x < validValues.length; ++x) { if(itemOrdered == validValues[x]) validItem = true; } Java Programming, Seventh Edition

Using Parallel Arrays Parallel array An alternative for searching One with the same number of elements as another The values in corresponding elements are related An alternative for searching Use the while loop Java Programming, Seventh Edition

Using Parallel Arrays (cont’d.) Figure 8-9 The FindPrice application that accesses information in parallel arrays Java Programming, Seventh Edition

Using Parallel Arrays (cont’d.) Figure 8-11 A for loop with an early exit Java Programming, Seventh Edition

Searching an Array for a Range Match Searching an array for an exact match is not always practical Range match Compare a value to the endpoints of numerical ranges Find the category in which a value belongs Java Programming, Seventh Edition

Figure 8-13 The FindDiscount class Java Programming, Seventh Edition

Passing Arrays to and Returning Arrays from Methods Pass a single array element to a method Same as passing a variable Passed by value A copy of the value is made and used in the receiving method All primitive types are passed this way Java Programming, Seventh Edition

Passing Arrays to and Returning Arrays from Methods (cont’d.) Reference types The object holds a memory address where the values are stored The receiving method gets a copy of the array’s actual memory address The receiving method has the ability to alter the original values in array elements Java Programming, Seventh Edition

Figure 8-16 The PassArrayElement class Java Programming, Seventh Edition

Returning an Array from a Method A method can return an array reference Include square brackets with the return type in the method header Java Programming, Seventh Edition

You Do It Declaring an Array Initializing an Array Using a for Loop to Access Array Elements Creating a Class That Contains an Array of Strings Searching an Array Passing an Array to a Method Java Programming, Seventh Edition

Don’t Do It Don’t forget that the lowest array subscript is 0 Don’t forget that the highest array subscript is one less than the length Don’t forget the semicolon following the closing curly brace in an array initialization list Don’t forget that length is an array property and not a method Don’t place a subscript after an object’s field or method name when accessing an array of objects Java Programming, Seventh Edition

Don’t Do It (cont’d.) Don’t assume that an array of characters is a string Don’t forget that array names are references Don’t use brackets with an array name when you pass it to a method Java Programming, Seventh Edition

Summary Array Array names Shorten many array-based tasks length field A named list of data items All have the same type Array names Represent computer memory addresses Shorten many array-based tasks Use a variable as a subscript length field Contains the number of elements in an array Java Programming, Seventh Edition

Summary (cont’d.) You can declare arrays that hold elements of any type, including Strings and other objects Search an array to find a match to a value Perform a range match Pass a single array element to a method Java Programming, Seventh Edition