Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can,

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can, but would.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Sections 1-4.
1 CS100J 13 March 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 Arrays.
Chapter 10 Arrays. Topics Declaring and instantiating arrays Array element access Arrays of objects Arrays as method parameters Arrays as return values.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
1 11/8/06CS150 Introduction to Computer Science 1 Arrays Chapter 8 page 477 November 13, 2006.
CS100J 17 March 2005 Arrays. Reading: Secs 8.1, 8.2, 8.3. The last Java feature to study in this course Quote for the Day: Computer science has its field.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays.
Arrays Chapter 8 page /24/07CS150 Introduction to Computer Science 1 Arrays (8.1)  One variable that can store a group of values of the same.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Arrays Lists.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 10 Arrays and Collections.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
1 CS100J 25 October 2006 Arrays. Reading: Secs 8.1, 8.2, 8.3. Listen to the following lectures on loops on your Plive CD. They are only 2-3 minutes long,
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
Arrays : Objectives After you have read and studied this chapter, you should be able to –Manipulate a collection of data values, using an array. –Declare.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Array Basics An array is a collection of data.
Programs and Classes A program is made up from classes Classes may be grouped into packages A class has two parts static parts exist independently Non-static.
1 On (computational) simplicity We are trying to teach not just Java, but how to think about problem solving. Computer science has its field called computational.
Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall The index of the first position in an.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Memory Management in Java Computer Science 3 Gerb Objective: Understand references to composite types in Java.
Arrays Adapted from materials created by Dr. Donald Bell, Cal Poly 2000 (updated February 2004)
2011 Calendar Important Dates/Events/Homework. SunSatFriThursWedTuesMon January
Lecture 8 – Array (Part 1) FTMK, UTeM – Sem /2014.
Arrays Collections of data Winter 2004CS-1010 Dr. Mark L. Hornick 1.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Chapter 10 Arrays Animated Version
Computer Organization and Design Pointers, Arrays and Strings in C
Hassan Khosravi / Geoffrey Tien
Primitive and Reference Data Values
Java Review Most of these slides are based on
Java Review: Reference Types
Arrays and Pointers CSE 2031 Fall September 2018.
Advanced Programming Behnam Hatami Fall 2017.
Arrays, Collections and Repetition Part A – Arrays and Repetition
CMSC 202 Static Methods.
Basic Files + (Chapter 10 – Arrays) : Objectives
Primitive and Reference Data Values
Chapter 6: Arrays.
Pseudocode and Flowcharts
Chapter 10 Arrays ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 6: Arrays.
Classes and Objects 5th Lecture
Lecture 18 Arrays and Pointer Arithmetic
Arrays and Collections
Java Classes and Objects 3rd Lecture
Introduction to Object-Oriented Programming with Java--Wu
Chapter 7 The Java Array Object © Rick Mercer.
Programs and Classes A program is made up from classes
Java Review Most of these slides are based on
McDonald’s calendar 2007.
7 Arrays.
Classes and Objects Static Methods
Java Programming Language
Arrays and Pointers (part 2)
Arrays and Pointers (part 2)
McDonald’s calendar 2007.
Classes and Objects Object Creation
Habitat Changes and Fish Migration
SPL – PS2 C++ Memory Handling.
2015 January February March April May June July August September
Habitat Changes and Fish Migration
Presentation transcript:

Chapter 9 Array Basics Suppose you need to process daily temperatures for a 12-month period in a science project, would you use 365 variables? You can, but would you? An array is a collection of data values of the same data type. If your program needs to deal with 100 integers, 365 real numbers, etc., you will use an array. Intro to OOP w/Java--Wu

Chapter 9 Primitive Arrays A primitive array stores multiple values of the same primitive data type. rainfall 1 2 3 4 5 6 7 8 9 10 11 The index of the first position in an array is 0. Intro to OOP w/Java--Wu

Arrays Variables are Reference Variables Chapter 9 Arrays Variables are Reference Variables byte short int double long float boolean String Applet MessageBox HiLo InputBox etc. char primitive reference Data Type A constant data value is represented graphically by a lock icon to convey the fact that the value is locked and cannot be changed. Primitive variables contain values Reference variables point at objects Intro to OOP w/Java--Wu

Arrays of Primitive Data Types Chapter 9 Arrays of Primitive Data Types Array Declaration <data type> [ ] <variable> //variation 1 <data type> <variable>[ ] //variation 2 Array Creation <variable> = new <data type> [ <size> ] Example double[] rainfall; rainfall = new double[12]; Variation 1 double rainfall [ ]; rainfall = new double[12]; Variation 2 As you can declare and create objects in one statement such as Person p = new Person( ); you can declare and create an array in one statement as double[ ] rainfall = new double[12]; Strictly speaking, an array is a reference data type and really an object because there is no class called Array in Java. The thumbnail note in page 413 is therefore not 100 percent accurate. An array is like an object! Intro to OOP w/Java--Wu

Accessing Individual Elements Chapter 9 Accessing Individual Elements Individual elements in an array accessed with the indexed expression. double[] rainfall = new double[12]; rainfall 1 2 3 4 5 6 7 8 9 10 11 rainfall[2] This indexed expression refers to the element at position #2 Intro to OOP w/Java--Wu

Example Programs Zadoot … out to reality … ArrayElements1.java

Array Lengths double[] rainfall = new double[12]; Chapter 9 Array Lengths double[] rainfall = new double[12]; double annualAverage; double sum = 0.0; int index; for (index = 0; index < rainfall.length; index++) { rainfall[index] = SavitchIn.readLineDouble(); sum += rainfall[index]; } annualAverage = sum / rainfall.length; The public constant length returns the capacity of an array. Notice the public constant length returns the capacity, not the actual number of non-blank values in the array. Intro to OOP w/Java--Wu

Example Programs Phrrud … out to reality … ArrayRain.java Phrrud … out to reality … ArrayAverages.java

Array Bounds Errors Trying to access an array element that does not exist causes a runtime error Negative indices Indices beyond the size Falop … out to reality … ArrayBoundsError.java

Chapter 9 Array Initialization Like other data types, it is possible to declare and initialize an array at the same time. int[] number = { 2, 4, 6, 8 }; double[] samplingData = { 2.443, 8.99, 12.3, 45.009, 18.2, 9.00, 3.123, 22.084, 18.08 }; String[] monthName = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" }; The capacity of the array is set to the number of elements in the list. number.length samplingData.length monthName.length 4 9 12 Intro to OOP w/Java--Wu

Example Programs Groeeet … out to reality … ArrayInit.java An array variable can be explicitly made to point to no data, using the null value Spaaocie … out to reality … ArrayNULL.java

References are Pointers A reference variable points to an object So, arrays are objects, but don't worry about that now But it does mean you can Have multiple references to an array Not copy an array with = Lose an array

Having Two References to an Array Chapter 9 Having Two References to an Array A. Variables are allocated in memory. clemens twain B. The reference to the new array is assigned to clemens. Array in Memory C. The reference in clemens is assigned to customer. A int[] clemens, twain; B clemens = new int[5]; C twain = clemens; Code State of Memory Intro to OOP w/Java--Wu

Example Program Dooop … out to reality … ArrayDup.java

Cloning an Array An array can be copied using the clone method Chapter 9 Cloning an Array An array can be copied using the clone method It's necessary to cast the clone to the right array type Babababoom … out to reality … ArrayClone.java Intro to OOP w/Java--Wu

Losing an Array customer A B C Code State of Memory Chapter 9 Losing an Array customer A. The variable is allocated in memory. B. The reference to the new array is assigned to customer. Array in Memory C. The reference to another array overwrites the reference in customer. Array in Memory A int[] customer; B customer = new int[5]; C customer = new int[5]; Code State of Memory Intro to OOP w/Java--Wu

Garbage Collection An array that has no references is garbage collected by the java program Spaaocie … out to reality … ArrayGC.java

Two-Dimensional Arrays Chapter 9 Two-Dimensional Arrays Two-dimensional arrays are useful in representing tabular information. Intro to OOP w/Java--Wu

Declaring and Creating a 2-D Array Chapter 9 Declaring and Creating a 2-D Array Declaration <data type> [][] <variable> //variation 1 <data type> <variable>[][] //variation 2 Creation <variable> = new <data type> [<size1>][<size2>] Example 1 2 3 4 payScaleTable = new double[4][5]; is really a shorthand for payScaleTable = new double[4][ ]; payScaleTable[0] = new double[5]; payScaleTable[1] = new double[5]; payScaleTable[2] = new double[5]; payScaleTable[3] = new double[5]; which is equivalent to for (int i = 0; i < 4; i++) { payScaleTable[i] = new double[5]; } double[][] payScaleTable; payScaleTable = new double[4][5]; 1 2 3 Intro to OOP w/Java--Wu

Example Programs Ieeei … out to reality … ArrayMatrix.java Ieeei … out to reality … ArrayCalendar.java Ieeei … out to reality … ArrayCube.java

Multi-dimensional Arrays … NOT Java does not really have multi-dimensional arrays Java has arrays of arrays int[][] data = new int[3][5]; is shorthand for int[][] data = new int[3][]; data[0] = new int[5]; data[1] = new int[5]; data[2] = new int[5];

Multi-dimensional Arrays in RAM int[][] data = new int[3][5]; Zuuu … out to reality … ArrayRAM.java

Irregular Arrays int[][] weirdData = new int[3][];

Irregular Arrays int[][] weirdData = new int[3][]; weirdData.length == 3 weirdData[1].length == 4 Jioooul … out to reality … ArrayIrreg1.java

Irregular Arrays … null int[][] data = new int[3][]; data[0] = new int[5]; data[1] = new int[4]; data[2] = null; Jioooul … out to reality … ArrayIrreg2.java

Multi-dimensional Array Initialization Chapter 9 Multi-dimensional Array Initialization Like 1D arrays, it is possible to declare and initialize a multi-dimensional array at the same time. Frong … out to reality … ArrayCubeInit.java Frong … out to reality … ArrayMDInit.java Intro to OOP w/Java--Wu

Passing Arrays to Methods - 1 Chapter 9 Passing Arrays to Methods - 1 Code A public int searchMinimum(float[] number)) { … } minOne = searchMinimum(arrayOne); At before searchMinimum A arrayOne A. Local variable number does not exist before the method execution When an array is passed to a method, only its reference is passed. A copy of the array is NOT created in the method. public int searchMinimum(float[] number) { int indexOfMinimum = 0; for (int i = 1; i < number.length; i++) { if (number[i] < number[indexOfMinimum]) { //found a indexOfMinimum = i; //smaller element } return indexOfMinimum; State of Memory Intro to OOP w/Java--Wu

Passing Arrays to Methods - 2 Chapter 9 Passing Arrays to Methods - 2 Code public int searchMinimum(float[] number)) { … } B minOne = searchMinimum(arrayOne); arrayOne The address is copied at B number arrayOne B. The value of the argument, which is an address, is copied to the parameter. State of Memory Intro to OOP w/Java--Wu

Passing Arrays to Methods - 3 Chapter 9 Passing Arrays to Methods - 3 Code public int searchMinimum(float[] number)) { … } C minOne = searchMinimum(arrayOne); arrayOne number While at inside the method C C. The array is accessed via number inside the method. State of Memory Intro to OOP w/Java--Wu

Passing Arrays to Methods - 4 Chapter 9 Passing Arrays to Methods - 4 Code public int searchMinimum(float[] number)) { … } minOne = searchMinimum(arrayOne); D arrayOne At after searchMinimum D arrayOne number D. The parameter is erased. The argument still points to the same object. State of Memory Intro to OOP w/Java--Wu

Example Programs Flunot … out to reality … ArrayParamAvg.java Flunot … out to reality … ArrayParam1.java

Returning Arrays Array variables in methods exist until the method ends, but the array data lives while referenced An array variable can be returned from a method The receiving array variable then refers to the array data, and the array persists Wrrbbrack … out to reality … ArrayReturn.java Wrrbbrack … out to reality … ArrayParam2.java

Local arrays Array variables in methods exist until the method ends The array data referred to by such an array variable is lost and garbage collected when the method ends Dessserts … out to reality … ArrayLocalGC.java