Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Chapter 8: Arrays.
Arrays I Savitch Chapter 6.1: Introduction to Arrays.
Arrays. What is an array An array is used to store a collection of data It is a collection of variables of the same type.
1 Fall 2008ACS-1903 Chapter 8: Arrays 8.1 – 8.8 Introduction to Arrays Processing Array Contents Passing Arrays as Arguments to Methods Some Useful Array.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
© The McGraw-Hill Companies, 2006 Chapter 5 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.
 Pearson Education, Inc. All rights reserved Arrays.
Lecture 14 The String Class Lecture 14 The String Class Instructor: Craig Duckett.
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.
Lecture 12 Instructor: Craig Duckett ARRAYS. Announcements Assignment 3 Assignment 3 Revision Assignment 4 (and Final Exam) GRADED! RETURNED! Woot! NEXT.
Chapter 7: Arrays and the ArrayList Class
© 2012 Pearson Education, Inc. All rights reserved. Lecture 2 (Chapter 8): Arrays and the ArrayList Class Starting Out with Java: From Control Structures.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: From Control Structures through Objects Third Edition.
One Dimensional Array. Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection.
Chapter 7: Arrays and the ArrayList Class
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Arrays and the ArrayList Class Starting Out with Java From Control.
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.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
Chapter 8: Arrays.
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 8: Arrays and the ArrayList Class Starting Out with Java: From.
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.
I NTRODUCTION TO PROGRAMMING Starting Out with Java: From Control Structures through Objects.
Arrays BCIS 3680 Enterprise Programming. Overview 2  Array terminology  Creating arrays  Declaring and instantiating an array  Assigning value to.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Arrays Arrays in C++ An array is a data structure which allows a collective name to be given to a group of elements which all have.
Chapter 5 Arrays. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 5-2 Learning Objectives  Introduction to Arrays  Declaring and referencing.
 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.
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.
BIT115: Introduction to Programming
Two Dimensional Arrays Found in chapter 8, Section 8.9.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
For Friday Read No quiz Program 6 due. Program 6 Any questions?
CHAPTER 6 ARRAYS IN C 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 F. Alakeel.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
Lecture 5 array declaration and instantiation array reference
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter Topics Chapter 7 discusses the following main topics:
Arrays, Searching and Sorting
Chapter 7A: Arrays and the ArrayList Class
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.
BIT115: Introduction to Programming
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays in Java.
Dr. Sampath Jayarathna Cal Poly Pomona
Programming Fundamentals
Presentation transcript:

Introduction to Collections Arrays

Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of homogeneous elements of fixed size. In an array, there is one name for the set of values…each element of the array is accessed using a subscript (or index). Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #2

An array of integer elements Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #3 anArray XX – anArray[0] YY – anArray[4] anArray.length is 9 (9 elements, 0 through 8

Creating Arrays An array is an object so it needs an object reference. int[] numbers; //declares a reference to an array that will hold integers. The next step creates the array and assigns its address to the numbers variable numbers = new int[6]; //creates a new array that will hold 6 integers. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #4 Array element values are initialized to 0. Array indexes always start at 0. 0 index 0 0 index 1 0 index 2 0 index 3 0 index 4 0 index 5

Creating Arrays Arrays may be of any type, not just int. float[] temperatures; char[] letters; long[] units; double[] sizes; Arrays may be of reference types. String [] words; Players [] list; Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #5

Instantiating Arrays The array size must be a non-negative number. It may be a literal value or be derived from a constant or variable. final int ARRAY_SIZE = 6; int[] numbers; numbers = new int[ARRAY_SIZE]; Once created, an array size is fixed and cannot be changed. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #6

Accessing the Elements of an Array An array is accessed by: ◦ the reference name ◦ a subscript that identifies which element in the array to access. ◦ hence the term, subscripted variable. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #7 numbers[0] = 20; //pronounced “numbers sub zero” numbers[0] 0 numbers[1] 0 numbers[2] 0 numbers[3] 0 numbers[4] 0 numbers[5] 20

Array elements may be used anywhere that a similar single variable may be used. may be on the left side of an assignment (the recipient) may be in any expression may be passed to a method may be returned from a method Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #8

Inputting and Outputting Array Elements Array elements can be treated as any other variable. They are simply accessed by the same name and a subscript. Example: ArrayDemo1.javaArrayDemo1.java Array subscripts can be accessed using variables (such as for loop counters). Example: ArrayDemo2.javaArrayDemo2.java Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #9

Bounds Checking Array indexes always start at zero and continue to (array length - 1). int values; values = new int[10]; This array would have indexes 0 through 9. Example: InvalidSubscript.javaInvalidSubscript.java Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #10

Off-by-One Errors It is very easy to be off-by-one when accessing arrays. // This code has an off-by-one error. int[] numbers; numbers = new int[100]; for (int i = 1; i <= 100; i++) numbers[i] = 99; Here, the equal sign allows the loop to continue on to index 100, where 99 is the last index in the array. This code would throw an ArrayIndexOutOfBoundsException. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #11

Array Initialization When relatively few items need to be initialized, an initialization list can be used to initialize the array. int[]days = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; The numbers in the list are stored in the array in order: ◦ days[0] is assigned 31, ◦ days[1] is assigned 28, ◦ days[2] is assigned 31, ◦ days[3] is assigned 30, ◦ etc. Example: ArrayInitialization.javaArrayInitialization.java Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #12

Alternate Array Declaration Previously we showed arrays being declared: int[] numbers; ◦ However, the brackets can also go here: int numbers[]; ◦ These are equivalent but the first style is typical. Multiple arrays can be declared on the same line. int[] numbers, codes, scores; With the alternate notation each variable must have brackets. int numbers[], codes[], scores; ◦ The scores variable in this instance is simply an int variable. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #13

Processing Array Contents Array elements can be used in relational operations: if(cost[20] < cost[0]) { //statements } They can be used as loop conditions: while(value[count] != 0) { //statements } Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #14

Array Length Arrays are objects and provide a public field named length that is a constant that can be tested. double[] temperatures = new double[25]; ◦ The length of this array is 25. The length of an array can be obtained via its length attribute. int size = temperatures.length; ◦ The variable size will contain 25. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #15

Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #16 STOP

The Enhanced for Loop Simplified array processing (read only) Always goes through all elements General: for(datatype elementVariable : array) statement; Example: int[] numbers = {3, 6, 9}; for(int val : numbers) { System.out.println(“The next value is” + val); } Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #17

Array Size The length attribute can be used in a loop to provide automatic bounding. for(int i = 0; i < temperatures.length; i++) { System.out.println(“Temperature “ + i “: “ + temperatures[i]); } Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #18 Index subscripts start at 0 and end at one less than the array length.

Array Size It is possible to get the size of an array from a user: String input; int numTests; int[] tests; Scanner keyboard = new Scanner(System.in); System.out.print("How many numbers do you have? "); input = keyboard.readLine(); numTests = Integer.parseInt(input); tests = new int[numTests]; Example: DisplayTestScores.javaDisplayTestScores.java Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #19

Passing Array Elements to a Method When a single element of an array is passed to a method it is handled like any other variable. Example: PassElements.javaPassElements.java More often you will want to write methods to process array data by passing the entire array, not just one element at a time. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #20

String Arrays Arrays are not limited to primitive data. An array of String objects can be created: String[] names = { "Bill", "Susan", "Steven", "Jean" }; Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #21 The names variable holds the address to the array. A String array is an array of references to String objects. Address “Bill” “Susan” “Steven” “Jean” address names[1] names[0] names[3] names[2] Example: MonthDays.java MonthDays.java

String Arrays If an initialization list is not provided, the new keyword must be used to create the array: String[] names = new String[4]; Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #22 The names variable holds the address to the array. Address null names[1] names[0] names[3] names[2]

String Arrays When an array is created in this manner, each element of the array must be initialized. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #23 The names variable holds the address to the array. Address null names[1] names[0] names[3] names[2] names[0] = "Bill"; names[1] = "Susan"; names[2] = "Steven"; names[3] = "Jean"; “Bill” “Susan” “Steven” “Jean”

Calling String Methods On Array Elements String objects have several methods. ◦ toUpperCase, ◦ compareTo ◦ equals ◦ charAt Each element of a String array is a String object. Methods can be used by using the array name and index as before. System.out.println(names[0].toUpperCase()); char letter = names[3].charAt(0); Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved. Cha pter 8 Slid e #24