Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming Lecture 5 Array and Collections.
Advertisements

Introduction to C Programming
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,
1 Various Methods of Populating Arrays Randomly generated integers.
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.
Chapter 10 Introduction to Arrays
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
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.
8. Arrays 8.1 Using Arrays 8.2 Reference versus Value Again 8.3 Passing Array Arguments and Returning Arrays 8.4 An Example: Simulating Rolls of the Dice.
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 Arrays Chapter 6.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Array Must declare a variable to reference the array double [] mylist; // cannot double list[20]; Or double mylist[]; The declaration doesn’t allocate.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
Computer Science 210 Computer Organization Pointers.
Question Hot Seat public MyConstructor (int a, int b) { index = a+b; } A.Parameters B.Class Name C.Body D.Modifier Please put match these choices to these.
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.
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Part 2. Searching Arrays Looking for a specific element in an array E.g., whether a certain score (85) is in a list of scores Linear search Binary search.
Ruby! Useful as a scripting language – script: A small program meant for one time use – Targeted towards small to medium size projects Use by: – Amazon,
Arrays II Robin Burke IT 130. Outline Homework #6 Portfolio Array review Array examples Array algorithms Multi-dimensional arrays.
1 Chapter 4: Arrays and strings Taufik Djatna
Array Processing - 2. Objectives Demonstrate a swap. Demonstrate a linear search of an unsorted array Demonstrate how to search an array for a high value.
JAVA Arrays. Objectives Be able to declare and initialize arrays Be able to conceptualize (draw) how arrays are represented in computer memory. Be able.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays Part 4.
11/15: Ch. 7: Arrays What is an array? Declaring & allocating arrays Sorting & searching arrays.
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
Computer Science 210 Computer Organization Arrays.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Arrays Pepper. What is an Array A box that holds many of the exact same type in mini-boxes A number points to the mini-box The number starts at 0 String.
1 Arrays An array is a collection of data values, all of which have the same type. The size of the array is fixed at creation. To refer to specific values.
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
1 Array basics. Data Structures Sometimes, we have data that have some natural structure to them  A few examples: Texts are sequences of characters Images.
Lecture 15: Projects Using Similar Data. What is an Array? An array is a data structure consisting of related data items of the same type. Stored in a.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
***** 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.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
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.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
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.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
Arrays, cont MONDAY – APRIL 6, Review from lab Deep vs Shallow copy array1array array2 = array1; What happens?
Computer Programming Arrays 1. Question #1 2 Question Choose the correct answer..
Arrays and Sorting. Process Open a file that contains integers, one per line. Read each line, convert to short and store each into an array Sort the array.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays Chap. 9 Storing Collections of Values 1. Introductory Example Problem: Teachers need to be able to compute a variety of grading statistics for.
Arrays. What is an array? An array is a collection of data types. For example, what if I wanted to 10 different integers? int num1; int num2; int num3;
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Sections 10.1 – 10.4 Introduction to Arrays
Computer Science 210 Computer Organization
Fundamentals of Programming II Working with Arrays
Building Java Programs
Building Java Programs
Computer Science 210 Computer Organization
Peer Instruction 6 Java Arrays.
Building Java Programs Chapter 7
Building Java Programs
Building Java Programs
Building Java Programs
Java Lesson 36 Mr. Kalmes.
CS2011 Introduction to Programming I Arrays (II)
Lecture 10: Arrays AP Computer Science Principles
Mr. Dave Clausen La Cañada High School
Suggested self-checks: Section 7.11 #1-11
Building Java Programs
Building Java Programs
Presentation transcript:

Arrays, Methods, Error Handling

Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray, aTarget); // Index of target int s = sum(anArray); // Sum of data abs(anArray); // Convert items to // absolute values

Form of Method Header ( [] ) Use [] to allow arrays of any size

Example: Linear Search int search (int[] array, int target){ // Returns index of first target found, or -1 otherwise. for (int i = 0; i < array.length; i++) if (array[i] == target) return i; return -1; } int[] array1 = {45, 67, 100, 22, 88}; System.out.println(search(array1, 100)); int[] array2 = {45, 67, 100, 22, 88, 55, 99}; System.out.println(search(array2, ));

Use Logical Size int search (int[] array, int target, int logicalSize){ // Returns index of first target found, or -1 otherwise. for (int i = 0; i < logicalSize; i++) if (array[i] == target) return i; return -1; } int[] array1 = {45, 67, 100, 22, 88}; System.out.println(search(array1, 100, array1.length));

Example: Return a Sum int sum (int[] array, int logicalSize){ int result = 0; for (int i = 0; i < logicalSize; i++) result = result + array[i]; return result; } int[] array1 = {2, 2, 2, 2, 2}; System.out.println(sum(array1, array1.length)); int[] array2 = {3, 4}; System.out.println(sum(array2, array2.length));

Build a String from an Array String sum (String[] array, int logicalSize){ String result = ""; for (int i = 0; i < logicalSize; i++) result = result + array[i]; return result; } Accumulation is a common pattern that can be used with numbers or strings. String[] array = {"Hi", " there!"}; System.out.println(sum(array, array1.length));

Modifying an Array Parameter void abs(int[] formalArray){ // Converts each integer to its absolute value for (int i = 0; i < formalArray.length; i++) formalArray[i] = Math.abs(formalArray[i]); } int[] actualArray = {-2, 4, -5, 3}; abs(actualArray); actualArray formalArray

Returning an Array Value int[] abs(int[] formalArray){ int[] resultArray = new int[formalArray.length]; for (int i = 0; i < formalArray.length; i++) resultArray[i] = Math.abs(formalArray[i]); return resultArray; } int[] actualArray = {-2, 4, -5, 3}; int[] secondArray = abs(actualArray); actualArray formalArray resultArray secondArray

Form of Array Return Type [] ( )

Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = array1; array array2 Array is a reference type. Assignment copies a pointer.

Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = array1; array1[1] = 6; array array2 Aliasing can cause serious side effects.

Copying vs Assignment int[] array1 = {3, 4, 5}; int[] array2 = new int[3]; for (int i = 0; i < array2.length; i++) array2[i] = array1[i]; array array2 345 Work with distinct array objects. Use a loop to transfer data.

Resizing an Array Java arrays cannot be resized But we can –create a larger or smaller new array –transfer data from the original array to the new array –reset the original array variable to the new array

Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; numbers

Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; numbers tempArray

Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; for (int i = 0; i < numbers.length; i++) tempArray[i] = numbers[i]; numbers tempArray

Make the Array Twice as Large int[] numbers = {3, 2, 6, 4}; int[] tempArray = new int[numbers.length * 2]; for (int i = 0; i< numbers.length; i++) tempArray[i] = numbers[i]; numbers = tempArray; numbers tempArray Off to garbage collector

A General Method int[] resize(int[] anArray, double sizeFactor){ int[] tempArray = new int[(int)(anArray.length * sizeFactor)]; for (int i = 0; i< anArray.length; i++) tempArray[i] = anArray[i]; return tempArray; } int[] numbers = {3, 2, 6, 4}; numbers = resize(numbers, 2); // Double the length int[] copy = resize(numbers, 1); // Just a copy

Assignment Creates an Alias int[] numbers = {3, 2, 6, 4}; int[] alias = numbers; // An alias int[] copy = resize(numbers, 1); // A real copy numbers alias copy