BIT115: Introduction to Programming

34
BIT115: Introduction to Programming Lecture 17 Instructor: Craig Duckett Passing & Returning Arrays

description

BIT115: Introduction to Programming. Lecture 13. Instructor: Craig Duckett. Announcements. Assignment 3 Revision. Due TONIGHT Wednesday, August 13 th , by midnight. Assignment 4. Due next Wednesday, August 20 th , by midnight. Today’s Topics. Arrays (Recap) Arrays as Parameters - PowerPoint PPT Presentation

Transcript of BIT115: Introduction to Programming

BIT115: Introduction to Programming

BIT115:Introduction to ProgrammingLecture 17Instructor: Craig Duckett

Passing & Returning Arrays

2Assignment 3 Revision GRADED! RETURNED!

Assignment 4 AND Final Exam (LECTURE 19) Monday, March 14 NO REVISION

Extra Credit 01 (LECTURE 20) Wednesday, March 16

Assignment Dates (By Due Date)

Todays TopicsArrays (Recap)Arrays as ParametersReturning an ArrayAssignment 4 Basic and Advanced OverviewWALK-THROUGH: ICE 17.1And now ...The Quiz

Recap: ArraysWhat is an Array?Primitive variables are designed to hold only one value at a time.Arrays allow us to create a collection of like values that are indexed.An array can store any type of data but only one type of data at a time.An array is a list of data elements.Recap: Arraysint grade1 = 100;int grade2 = 89;int grade3 = 96;int grade4 = 100;int grade5 = 98;Lets pretend we have a list of five (5) grades wed like to store in memory. We could declare the value for each grade one at a time like this:The five grades are then stored in memory wherever there happens to be room. The address in memory where each grade is store is its declared name (e.g., grade1) and the data stored there is the integer value itself (e.g., 100). To access each grade number and do something with it (e.g., display it), wed call up the memory location by its assigned (declared) name:

System.out.println("The first grade is: " + grade1);Recap: ArraysWe could, however, simplify our work by doing the same thing with an array. Instead of declaring each integer one at a time, we could declare the whole shebang at the same time, like so: Initially, this creates a location in memory to hold the five elements that make up the array, automatically initializing each of those storage spaces to zero. All you have to do now is fill those elements.grades = new int[5]; grades[0] = 100; grades[1] = 89;grades[2] = 96;grades[3] = 100;grades[4] = 98; Of course, you could have also declared and set them up like this all in one step:

int[ ] grades = { 100, 89, 96, 100, 98 }; Even though you declared an array with [5] elements, the count of the elements starts with [0]

In memory, the array elements are mostly stored in sequential orderRecap: Arraysgrades[0] = 100; grades[1] = 89;grades[2] = 96;grades[3] = 100;grades[4] = 98; The entire collection of element identifiers is called the index

grades[0] = 100; grades[1] = 89;grades[2] = 96;grades[3] = 100;grades[4] = 98; Each individual element identifier in the index is called a subscriptRecap: Arraysgrades = new int[5];

grades[0] = 100; grades[1] = 89;grades[2] = 96;grades[3] = 100;grades[4] = 98; int grade1 = 100;int grade2 = 89;int grade3 = 96;int grade4 = 100;int grade5 = 98;Comparing individual integer declarations to an array of integers:System.out.println("The first grade is: " + grade[0]);System.out.println("The first grade is: " + grade1);

In memory, the array elements are mostly stored in sequential orderIn memory, the individual integers are stored wherever there happens to be an available spaceRecap: ArraysBecause array elements are numbered sequentially, it is a no-brainer to either populate the data in the array, or to call up and access the data in the array usingWith loops you can either enter data manually when prompted or enter data automatically based on the particular requirements of the data going into the arrayScanner keyboard = new Scanner(System.in); // Original code thanks to Kamran Majidimehr!int counter = 0;int arraylength = 0; System.out.println("How many grades would you like to enter?");arraylength = keyboard.nextInt();System.out.println("You have indicated that you'd like to enter " + arraylength + " grades.\nPlease enter grades now, one at a time."); int countdown = arraylength;double average = 0;double grades[] = new double [arraylength]; while( counter < arraylength ){ grades[counter] = keyboard.nextDouble(); System.out.println("(" + (counter + 1) + ") You entered: " + grades[counter] + " [There are " + (countdown - 1) + " grades remaining]"); counter++; countdown--; }LoopsArrays as Parameters

Passing shortArray by namePassing longArray by nameshortArray createdlongArray created

Arrays as Parametersclass ArrayHelper - PrintArray methodmain - shortArray - longArray

Arrays as ParametersPassing anarray as a parameter

This method has been setup to expect an array to be passed to it, and will use the array space called arrayName to point to the passed array data when it is.

Arrays as Parameters

Arrays as Parameters

Arrays as Parameters

Arrays as ParametersBut how does this really work in memory?

How does the method actually know which array to point to and use?1, 3, 5NOTE! Passing an array to a method behaves differently than passing a primitive data type! The array isnt copied to the method, the method points to the array data!

Arrays as Parameters

Is allocated whenprogram loads;temporaryIs allocated whenprogram runs;persistentDont worry about any of this memory, stack, or heap information as it wont show up on the Final Exam

Arrays as Parameters1, 3, 51, 2, 3, 5, 7shortArraylongArray(1) PrintArray method is called and and checks name in parameter which is a named storage space in the memory heap(2) PrintArray than takes the array element data as needed and plugs it through the arrayName placeholder into the body of the method where it can do its work on the same array!NOTE: When a primitive data type, e.g. int, is passed to a method, its value is COPIED to the new method variable. For reference data types, a new reference is created, but unlike for primitives, the data that is referenced is NOT copied to a new area of memory. Instead the new reference is set to REFER to the original area of memory storing the data. This is known as passing by reference. As such, when arrays are concerned, simply calling/using an array method can alter/change the data in the original array since they method is pointing to the same memory location as the original array.So, when you pass an array as a parameter, youre actually passing the address of where the array data is located, and then that data is copied to the methods array space to be used.

AN ABSTRACTION:How Does This Look In Memory?

AN ABSTRACTION:How Does This Look In Memory?Arrays as Parametersimport java.util.*;

class Change extends Object{ public void AddFives(int arrayName[]) { for(int j = 0; j < arrayName.length; j++) { arrayName[j]+=5; // Same as: arrayName[j] = arrayName[j] + 5; } }}

public class PassArrayChangeValues extends Object{ public static void main(String[] args) { Change DoIt = new Change(); int [] myArray = new int[5]; myArray[0] = 2; myArray[1] = 4; myArray[2] = 6; myArray[3] = 8; myArray[4] = 10; System.out.println("myArray contains:"); for(int i = 0; i < myArray.length; i++) { System.out.println("Slot " + (i + 1) + " is: " + myArray[i]); } DoIt.AddFives(myArray); System.out.println(); System.out.println("myArray NOW contains:"); for(int i = 0; i < myArray.length; i++) { System.out.println("Slot " + (i + 1) + " is: " + myArray[i]); } }}PassArrayChangeValues.java22Lecture 18: Returning an ArrayIm going to go over the Lecture 18 part of this now, and repeat it again on Monday where you will do the ICES for Returning an Array at that time.Passing and Returning ArraysPassing Arrays as ArgumentsArrays are objects.Their references can be passed to methods like any other object reference variable.510152025AddressshowArray(numbers);303540public static void showArray(int[] array){ for (int i = 0; i < array.length; i++) System.out.print(array[i] + " ");}Example: PassArray.javaPassing and Returning ArraysIn Last Wednesdays Class we learned how you could pass an array into a method. Today were going to take this to the next step to learn how you can return an array from a method.

A method can return a reference to an array. To do so, the return type of the method must be declared properly. For example, look at the following method definition:

public static double[] getArray() //