Arrays, cont MONDAY – APRIL 6, 2015. Review from lab Deep vs Shallow copy array1array2 25.5 23 15.8 19 22 array2 = array1; What happens?

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to Java 2 Programming Lecture 5 Array and Collections.
Computer Science 209 Software Development Equality and Comparisons.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Week 8 Arrays Part 2 String & Pointer
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
Winter 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assn 4 posted shortly. Demonstrate a memory leak problem using the assignment 4 solution. Back to.
CSE 115 Week 11 March , Announcements March 26 – Exam 7 March 26 – Exam 7 March 28 – Resign Deadline March 28 – Resign Deadline Lab 7 turned.
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Programming with Collections Collections in Java Using Arrays Week 9.
Introduction to Programming with Java, for Beginners Arrays of Objects.
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Chapter 9 Introduction to Arrays
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Programming in C Structs and Unions. No Classes in C Because C is not an OOP language, there is no way to combine data and code into a single entity.Because.
Introduction to Arrays. Useful Array Operations  Finding the Highest Value int [] numbers = new int[50]; int highest = numbers[0]; for (int i = 1; i.
Multiple Choice Solutions True/False a c b e d   T F.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Searching Arrays Searching.
Java Arrays …………. Java Arrays …………. * arrays are objects in Java * arrays are objects in Java * an array variable is a reference * an array variable is.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Chapter 8: Arrays.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
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.
SINGLE-DIMENSION ARRAYS. Data Structures It is a collection of scalar data types that are all referenced or accessed through one identifier – scalar type.
CS 206 Introduction to Computer Science II 09 / 10 / 2009 Instructor: Michael Eckmann.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
CompSci Arrays  Aggregate data type  Deal with items of same type  Lists of words  Numbers  Analogies  Mailboxes in post office  CD racks.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
In Java.... Variables contain the values of primitive data types Value Types.
Chapter 6 Interfaces. Class Status CU will be close for winter break from Dec. 22. till Jan.2 We have 3 classes left after tonight (Jan 8,15, and 22)
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays, Methods, Error Handling. Using Arrays as Parameters int[] anArray = ; int index = min(anArray); // Index of smallest int index = search(anArray,
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Array Declarations Arrays contain a fixed number of variables of identical type Array declaration and allocation are separate operations Declaration examples:
A: A: double “4” A: “34” 4.
Written by: Dr. JJ Shepherd
11 PART 2 ARRAYS. 22 PROCESSING ARRAY ELEMENTS Reassigning Array Reference Variables The third statement in the segment below copies the address stored.
CSCI 130 More on Arrays. Multi-dimensional Arrays Multi - Dimensional arrays: –have more than one subscript –can be directly initialized –can be initialized.
Week 10 - Wednesday.  What did we talk about last time?  Method example  Roulette simulation  Types in Java.
CMSC 150 LOOPS CS 150: Fri 20 Jan Representing DNA AGTCCAGTGTCAA.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Arrays What is an array… –A data structure that holds a set of homogenous elements (of the same type) –Associate a set of numbers with a single variable.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Beginning C for Engineers Fall 2005 Arrays, 2-D arrays, character strings Bettina Schimanski Lecture 5: Section 2 (9/28/05) Section 4 (9/29/05)
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Data Structures & Algorithms CHAPTER 2 Arrays Ms. Manal Al-Asmari.
Common Elementary Algorithms Some of the basic but frequently used algorithms for manipulating arrays. These algorithms are so important that: a)Some programming.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Chapter 5 Arrays F Introducing Arrays F Declaring Array Variables, Creating Arrays, and Initializing Arrays F Passing Arrays to Methods F Copying Arrays.
Array Review CS 139 – November 28, 2007.
Java for Beginners University Greenwich Computing At School DASCO
Array Review CS 139 – November 28, 2007.
Value Types and Reference Types
Object Oriented Programming in java
Array Review CS 139 – November 28, 2007.
More on Structs Sometimes we use structs even when the fields are all of the same type. If the fields are different conceptually, that is, the data stands.
Presentation transcript:

Arrays, cont MONDAY – APRIL 6, 2015

Review from lab Deep vs Shallow copy array1array array2 = array1; What happens?

Shallow copy  The reference is copied not the object itself  The two variables are called “aliases” since they both refer to the same object.  When passing objects to a method, a shallow copy is performed. (This includes arrays).

Deep copy array2 = new double[array1.length]; for (int idx = 0; idx < array1.length; idx++) { array2[idx] = array1[idx]; } What happens? array2array1

Deep copy array2array

Concurrent (parallel) arrays String [] months = {“Jan”, “Feb”, “Mar”, … “Dec”}; int[] days = {31, 28, 31, … 31}; for (int ii = 0; ii < months.length; ii++) { S.O.P(months[ii] + “ has “ + days[ii] + “ days.”) } See MonthDays.java

Arrays of Objects  Declare the array;  Instantiate the array;  Instantiate the objects of the array;  See ObjectArray.java

Now for the coolest thing ever! VarargsDemo1.java

And the enhanced for loop See DisplayTestScores.java

Array Methods See the Java API l/Arrays.htmlhttps://docs.oracle.com/javase/7/docs/api/java/uti l/Arrays.html API stands for Application Program Interface Everything you need to know to use classes in the Java libraries.

Comparing arrays Write a method, areSame, that returns true if the two arrays, array1 and array2 contain the exact same values and false otherwise. array1 and array2 are arrays of char type. Note: if one or the other of the arrays are null, return false. If they are both null, return true.

Sum the values Write a method, sumArray, that takes in a single array of double and returns the sum of the elements in the array. If the array of double is null, return Double.MIN_VALUE;

Find a given value Write a java method, findString, that takes in an array of String objects and a String search key and returns an int with the location of the search key in the array. Return -1 if the key is not found in the array.

Find the highest value Write a method, highest, which takes in an array of int and returns the largest int in the array. If the array is null or has no elements, return Integer.MIN_VALUE;

Combining arrays Write a method, arrayConcat which takes in two double arrays and returns a new array which is the first array concatenated with the second (in other words, the array will contain all of the elements of the first followed by all of the elements of the second. It should work properly if either of the arrays is null. If both are null, the method should return null.