Array Review CS 139 – November 28, 2007.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

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.
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.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
© 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.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
8-1 Chapter-7 Part1 Introduction to Arrays –Array Types –Creating/Declaring Arrays –Initializing Arrays –Processing Array Contents –Passing Arrays as Arguments.
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Chapter 7: Arrays and the ArrayList Class Starting Out with Java: From Control Structures through Objects Fifth Edition by Tony Gaddis.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
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 8 Arrays and the ArrayList Class Arrays of Objects.
Lecture 5 Arrays A way to organize data. What are Arrays? An array is a named list of data values that all have the same type An array is a collection.
Chapter 8 Arrays and the ArrayList Class Introduction to Arrays.
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
Pointers and Dynamic Arrays
Array Review CS 139 – November 28, 2007.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter Topics Chapter 7 discusses the following main topics:
Parameter Lists & Command Line Arguments
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Selenium WebDriver Web Test Tool Training
Arrays, Searching and Sorting
if-else-if Statements
all loops initialization – set up the loop
CMSC 202 Static Methods.
BIT115: Introduction to Programming
Arrays ICS 111: Introduction to Computer Science I
Chapter 17 Linked Lists.
Chapter 4 Inheritance.
Chapter 6 Arrays Solution Opening Problem
Chapter 7A: Arrays and the ArrayList Class
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
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.
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 4 Writing Classes.
Defining methods and more arrays
Arrays.
Chapter 6 Arrays.
Single-Dimensional Arrays chapter6
Chapter 6 Data Types.
Array Review CS 139 – November 28, 2007.
Dr. Sampath Jayarathna Cal Poly Pomona
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Arrays in Java.
Suggested self-checks: Section 7.11 #1-11
Dr. Sampath Jayarathna Cal Poly Pomona
Chapter 9: Pointers and String
Chapter 5 Linked Lists © 2006 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Multidimensional Arrays Section 6.4
Chapter 6 Arrays.
Programming Fundamentals
Chapter 2 Reference Types.
Chapter 4 Greedy Algorithms.
Presentation transcript:

Array Review CS 139 – November 28, 2007

Terminology – See html page Array Element Subscript Instantiation Initializer list

Array Model int anArray; anArray = new int []; anArray Note: the array elements are initialized to 0 since this is a numeric type and the constructor will initialize numeric fields to 0.

Arrays and loops Before: After: anArray Before: for (int ii = 0; ii < anArray.length; ii++) anArray[ii] = ii; After: 1 2 3 4 5

Checkpoint Do checkpoint 8.1, 8.3, 8.4, 8.6, 8.8

Array length Arrays are objects. As such, arrays “know themselves”. An array knows how many elements it contains. length is a field of the array (or an attribute of the array). NOTE: no ()…length is a method in the String class, an attribute or field in the array class.

length and subscripts We can use length to determine how many elements to process in the array. The largest subscript is the length – 1. So for an array of 20 elements, there are 20 subscripts beginning with 0, so the highest subscript is 19. arrayName.length can be used to control array processing.

final int ARRAY_SIZE = 10; char myArray; myArray = new char[ARRAY_SIZE]; // updating an array – standard for loop for (int ii = 0; ii < myArray.length; ii++) myArray = ‘A’; // reading an array – enhanced for loop for (char letter : myArray) System.out.println(letter);

Enhanced for loop limitations May only be used on a single loop May only be used to “read” the elements of an array. May only process the array from 0 – length – 1; cannot process the array in reverse. Will process all array elements. You do not have access to the subscript.

Checkpoint Do 8.9 (page 454). First draw a model of the two arrays in question. Write the statement. Then write statements to create a new array, numbers3 which will have the same number of elements as numbers1 (do not count). Write a statement that will fill numbers3 with the product of the same position elements in numbers1 and numbers2. Do 8.12.

Reassigning Array References An array reference can be assigned to another array of the same type. // Create an array referenced by the numbers variable. int[] numbers = new int[10]; // Reassign numbers to a new array. numbers = new int[5]; If the first (ten element) array no longer has a reference to it, it will be garbage collected. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

Reassigning Array References int[] numbers = new int[10]; The numbers variable holds the address of an int array. Address Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

Reassigning Array References This array gets marked for garbage collection The numbers variable holds the address of an int array. Address numbers = new int[5]; Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

Copying Arrays You cannot copy an array by merely assigning one reference variable to another. You need to copy the individual elements of one array to another. int[] firstArray = {5, 10, 15, 20, 25 }; int[] secondArray = new int[5]; for (int i = 0; i < firstArray.length; i++) secondArray[i] = firstArray[i]; This code copies each element of firstArray to the corresponding element of secondArray. Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

Passing Arrays as Arguments Arrays are objects. Their references can be passed to methods like any other object reference variable. 5 10 15 20 25 Address showArray(numbers); 30 35 40 public static void showArray(int[] array) { for (int i = 0; i < array.length; i++) System.out.print(array[i] + " "); } Example: PassArray.java Starting Out With Java Control Structures to Objects By Tony Gaddis Copyright © 2005, Pearson Addison-Wesley. All rights reserved.

Arrays of Objects You must use three steps to create an array of object types. Declare the array Instantiate the array (What are we making when we instantiate the array?) Instantiate the objects that the array will hold This step does not need to happen all at once…think about Boxes

Checkpoint Given a Die class, create an array that will hold 5 Die objects. Roll each of the Die objects in the array. Display each element of the array. Reroll only the 2nd and 3rd elements of the array.