Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.

Slides:



Advertisements
Similar presentations
Generics and the ArrayList Class
Advertisements

The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 Overview 7.1 Introduction to Arrays 7.2 Arrays in Functions 7.3.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Arrays Chapter 6 Chapter 6.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
CS102--Object Oriented Programming Lecture 5: – Arrays – Sorting: Selection Sort Copyright © 2008 Xiaoyan Li.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CMSC 202 Java Primer. Version 9/09Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
Comp 248 Introduction to Programming Chapter 6 Arrays Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
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 8: Arrays.
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 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 8: Arrays and Functions Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
COMPUTER PROGRAMMING. Array C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Pointers and Dynamic Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 9 Pointers and Dynamic Arrays.
CMSC 202 Java Primer. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
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];
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Lesson 3 Functions. Lesson 3 Functions are declared with function. For example, to calculate the cube of a number function function name (parameters)
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Arrays 2/4 By Pius Nyaanga.
Chapter 6 Arrays Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
CISC181 Introduction to Computer Science Dr
Suggested self-checks: Section 7.11 #1-11
CSS161: Fundamentals of Computing
Classes, Objects and Methods
Chapter 4 Constructors Section 4.4
Classes and Objects Object Creation
CMSC 202 Constructors Version 9/10.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays

© 2006 Pearson Addison-Wesley. All rights reserved6-2 Arrays and References Like class types, a variable of an array type holds a reference –Arrays are objects –A variable of an array type holds the address of where the array object is stored in memory –Array types are (usually) considered to be class types

© 2006 Pearson Addison-Wesley. All rights reserved6-3 Arrays are Objects An array can be viewed as a collection of indexed variables An array can also be viewed as a single item whose value is a collection of values of a base type –An array variable names the array as a single item double[] a; –A new expression creates an array object and stores the object in memory new double[10] –An assignment statement places a reference to the memory address of an array object in the array variable a = new double[10];

© 2006 Pearson Addison-Wesley. All rights reserved6-4 Arrays Are Objects The previous steps can be combined into one statement double[] a = new double[10]; Note that the new expression that creates an array invokes a constructor that uses a nonstandard syntax Not also that as a result of the assignment statement above, a contains a single value: a memory address or reference Since an array is a reference type, the behavior of arrays with respect to assignment ( = ), equality testing ( == ), and parameter passing are the same as that described for classes

© 2006 Pearson Addison-Wesley. All rights reserved6-5 Pitfall: Arrays with a Class Base Type The base type of an array can be a class type Date[] holidayList = new Date[20]; The above example creates 20 indexed variables of type Date –It does not create 20 objects of the class Date –Each of these indexed variables are automatically initialized to null –Any attempt to reference any them at this point would result in a "null pointer exception" error message

© 2006 Pearson Addison-Wesley. All rights reserved6-6 Pitfall: Arrays with a Class Base Type Like any other object, each of the indexed variables requires a separate invocation of a constructor using new (singly, or perhaps using a for loop) to create an object to reference holidayList[0] = new Date();... holidayList[19] = new Date(); OR for (int i = 0; i < holidayList.length; i++) holidayList[i] = new Date(); Each of the indexed variables can now be referenced since each holds the memory address of a Date object

© 2006 Pearson Addison-Wesley. All rights reserved6-7 Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed variable can be an argument to a method in exactly the same way that any variable of the array base type can be an argument

© 2006 Pearson Addison-Wesley. All rights reserved6-8 Array Parameters double n = 0.0; double[] a = new double[10];//all elements //are initialized to 0.0 int i = 3; Given myMethod which takes one argument of type double, then all of the following are legal: myMethod(n);//n evaluates to 0.0 myMethod(a[3]);//a[3] evaluates to 0.0 myMethod(a[i]);//i evaluates to 3, //a[3] evaluates to 0.0

© 2006 Pearson Addison-Wesley. All rights reserved6-9 Array Parameters An argument to a method may be an entire array Array arguments behave like objects of a class –Therefore, a method can change the values stored in the indexed variables of an array argument A method with an array parameter must specify the base type of the array only BaseType[] –It does not specify the length of the array

© 2006 Pearson Addison-Wesley. All rights reserved6-10 Array Parameters The following method, doubleElements, specifies an array of double as its single argument: public class SampleClass { public static void doubleElements(double[] a) { int i; for (i = 0; i < a.length; i++) a[i] = a[i]*2;... }... }

© 2006 Pearson Addison-Wesley. All rights reserved6-11 Array Parameters Arrays of double may be defined as follows: double[] a = new double[10]; double[] b = new double[30]; Given the arrays above, the method doubleElements from class SampleClass can be invoked as follows: SampleClass.doubleElements(a); SampleClass.doubleElements(b); –Note that no square brackets are used when an entire array is given as an argument –Note also that a method that specifies an array for a parameter can take an array of any length as an argument

© 2006 Pearson Addison-Wesley. All rights reserved6-12 Pitfall: Use of = and == with Arrays Because an array variable contains the memory address of the array it names, the assignment operator ( = ) only copies this memory address –It does not copy the values of each indexed variable –Using the assignment operator will make two array variables be different names for the same array b = a; –The memory address in a is now the same as the memory address in b : They reference the same array

© 2006 Pearson Addison-Wesley. All rights reserved6-13 Pitfall: Use of = and == with Arrays A for loop is usually used to make two different arrays have the same values in each indexed position: int i; for (i = 0; (i < a.length) && (i < b.length); i++) b[i] = a[i]; –Note that the above code will not make b an exact copy of a, unless a and b have the same length

© 2006 Pearson Addison-Wesley. All rights reserved6-14 Pitfall: Use of = and == with Arrays For the same reason, the equality operator ( == ) only tests two arrays to see if they are stored in the same location in the computer's memory –It does not test two arrays to see if they contain the same values (a == b) –The result of the above boolean expression will be true if a and b share the same memory address (and, therefore, reference the same array), and false otherwise

© 2006 Pearson Addison-Wesley. All rights reserved6-15 Pitfall: Use of = and == with Arrays In the same way that an equals method can be defined for a class, an equalsArray method can be defined for a type of array –This is how two arrays must be tested to see if they contain the same elements –The following method tests two integer arrays to see if they contain the same integer values

© 2006 Pearson Addison-Wesley. All rights reserved6-16 Pitfall: Use of = and == with Arrays public static boolean equalsArray(int[] a, int[] b) { if (a.length != b.length) return false; else { int i = 0; while (i < a.length) { if (a[i] != b[i]) return false; i++; } return true; }

© 2006 Pearson Addison-Wesley. All rights reserved6-17 Example

© 2006 Pearson Addison-Wesley. All rights reserved6-18