***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a.

Slides:



Advertisements
Similar presentations
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Advertisements

The University of Adelaide, School of Computer Science
1 Chapter Thirteen Pointers. 2 Pointers A pointer is a sign used to point out the direction.
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading: , 3.3 self-checks: Ch. 7 #5, 8,
Week 8 Arrays Part 2 String & Pointer
The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Arrays and ArrayLists C H A P T E R slides partially adapted from.
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.
Procedures and Control Flow CS351 – Programming Paradigms.
Kernighan/Ritchie: Kelley/Pohl:
Arrays Programming COMP102 Prog. Fundamentals I: Arrays / Slide 2 Arrays l An array is a collection of data elements that are of the same type (e.g.,
Topic 9A – Arrays as Function Arguments. CISC105 – Topic 9A Arrays as Function Arguments There are two ways to use arrays as function arguments: Use an.
Introduction to C Programming CE
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
CSE 1301 J Lecture 13 Sorting Richard Gesick. CSE 1301 J 2 of 30 Sorting an Array When an array's elements are in random order, our Sequential Search.
Multiple Choice Solutions True/False a c b e d   T F.
1 Chapter 7 Single-Dimensional Arrays. 2 Arrays Array is a data structure that represents a collection of the same types of data elements. A single-dimensional.
More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
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.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
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.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Topic 22 arrays - part 2 Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Lecture 3 Classes, Structs, Enums Passing by reference and value Arrays.
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Arrays as Parameters reading:
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Lab 8. Declaring and Creating Arrays in One Step datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10];
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
COP 3540 Data Structures with OOP
142 L -1 Pointers Chapter 6 C uses a call BY VALUE for functions for example: void add_one(int x, int y) { x=x+1; y=y+1; } int main(void) { int a,b; a=4;
Chapter 6 Arrays 1 Fall 2012 CS2302: Programming Principles.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSE 251 Dr. Charles B. Owen Programming in C1 Intro to Arrays Storing List of Data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
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.
Building Java Programs
Chapter 7 Single-Dimensional Arrays
Array traversals, text processing
CSC 253 Lecture 8.
Chapter 6 Arrays Solution Opening Problem
CSC 253 Lecture 8.
Building Java Programs
Introducing Arrays Array is a data structure that represents a collection of the same types of data.
Chapter 6 Arrays Fall 2012 CS2302: Programming Principles.
Chapter 5 Arrays Introducing Arrays
Return by Reference CSCE 121 J. Michael Moore.
2 code samples int [] array; int i, j, temp; for(i = 0; i < array.length/2; i++) { j = array.length-1-i; temp = array[i]; array[i] = array[j]; array[j]
Simulating Reference Parameters in C
Chapter 6 Arrays.
Why did the programmer quit his job?
Single-Dimensional Arrays chapter6
Chapter 6 Arrays.
Intro to Arrays Storing List of Data.
Presentation transcript:

***** SWTJC STEM ***** Chapter 7 cg 50 Arrays as Parameters Regular variables are passed to a method by value; i.e., the variable value is copied to a temporary variable on the memory stack. If the method variable is changed, it has no effect on the calling variable. Only the memory stack copy is changed. Recall also that, while methods can accept multiple parameters, they can only return one value. Arrays overcome this restriction. Array variables are passed to a method by reference; i.e., only a pointer to the calling array is passed, which means that any changes to the method’s array will change the calling array also. Thus, arrays permit methods to, in a sense, “return” multiple values.

***** SWTJC STEM ***** Chapter 7 cg 50 Arrays Parameters Example First we will pass an array and return a single value. Create the array. int[ ] myList2 = {0, 1, 2, 3, 4}; Call the sumArray method in the class MyArrayUtility to sum the elements of the array. System.out.println("The sum of elements is " + MyArrayUtility.sumArray(myList2)); The output is: The sum of elements is 10

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b sumArray Method Calling statement (passes array myList2): System.out.println("The sum of elements is " + MyArrayUtility.sumArray(myList2)); Called method (returns single variable sum): public static int sumArray(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) sum+=array[i]; return sum; }

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Arrays as Parameters Keep in mind that a “calling” array is not passed to the memory stack; only a reference pointer is passed! Thus, the sumArray method’s array is actually myList2; i.e., changing array changes myList2. In the next example, we pass an array, manipulate it, and “return” the changed array. Consider this example: “Code a method to reverse the elements of an array; i.e., first and last elements swapped, second and next to last elements swapped, etc.” We don’t actually pass and return the array. We pass only the pointer, so in the called method, we are actually manipulating the original array!

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method Create the array. int[ ] myList2 = {0, 1, 2, 3, 4}; Call the reverseArray method in the class MyArrayUtility to reverse the elements of the array. MyArrayUtility.reverseArray(myList2)); The value of array element 0 is 4 The value of array element 1 is 3 The value of array element 2 is 2 The value of array element 3 is 1 The value of array element 4 is 0

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Swapping Variables Temp = A A = B B = Temp

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method public static void reverseArray(int[] array) { int temp, j = array.length - 1; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } 0(4) (0) 0 i = 0 j = 4 temp

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method public static void reverseArray(int[] array) { int temp, j = array.length - 1; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } 4 1(3) 2 3(1) 0 1 i = 1 j = 3 temp

***** SWTJC STEM ***** Chapter 7 cg 50 Chapter 7 Part b Reverse Array Method public static void reverseArray(int[] array) { int temp, j = array.length - 1; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[j]; array[j] = temp; j--; } i = 2 j = 4 temp

***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Copying Arrays To copy a single variable, we use the assignment statement. heightCopy = height; // Works nicely. But does this work with arrays? Consider array myList: myListCopy = myList; // Not exactly! Array assignments copy the pointer only, which means that myListCopy is really pointing to the same array data as myList. Changing myListCopy will change myList. Thus, it is not an independent copy! To create an independent copy, use a for loop or the static arraycopy method of the System class.

***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Array Copy Example int myList1[] = {0, 1, 2, 3, 4}; int myList2[]; myList2 = myList1; // Attempt to copy myList1 to myList myList2[3] = 7; // Change element 3 of myList2 Did not Work!!!! Why? index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 3 index 4 myList1 4 myList2 4 Before index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 7 myList2 7 index 4 myList1 4 myList2 4 After

***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Array Copy For Loop int myList1[] = {0, 1, 2, 3, 4}; int[] myList2 = new int[myList1.length]; for(int i = 0; i < myList1.length; i++) myList2[i] = myList1[i];..... myList2[3] = 7; // Change only myList2???? Worked OK!!!! index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 3 index 4 myList1 4 myList2 4 Before index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 7 index 4 myList1 4 myList2 4 After

***** SWTJC STEM ***** Chapter 7 cg 51 Chapter 7 Part b Array Copy System int myList1[] = {0, 1, 2, 3, 4}; int[] myList2 = new int[myList1.length]; System.arraycopy(myList1, 0, myList2, 0, myList1.length);..... myList2[3] = 7; // Change only myList2???? Worked OK!!!! index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 7 index 4 myList1 4 myList2 4 After index 0 myList1 0 myList2 0 index 1 myList1 1 myList2 1 index 2 myList1 2 myList2 2 index 3 myList1 3 myList2 3 index 4 myList1 4 myList2 4 Before