ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
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.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Arrays Clark Savage Turner, J.D., Ph.D. Copyright © 2000 by C Scheftic. All rights reserved. These notes do rely heavily.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
ECE122 L17: Method Development and Testing April 5, 2007 ECE 122 Engineering Problem Solving with Java Lecture 17 Method Development and Testing.
Java Unit 9: Arrays Declaring and Processing Arrays.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
7. Arrays. Topics Declaring and Using Arrays Some Array Algorithms Arrays of Objects Variable Length Parameter Lists Two-Dimensional Arrays The ArrayList.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
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.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
CSE 501N Fall ‘09 08: Arrays 22 September 2009 Nicholas Leidenfrost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved.
Programming in Java (COP 2250) Lecture Chengyong Yang Fall, 2005.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 7: Arrays.
Chapter 7 Arrays: Part 1 of 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of.
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
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.
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.
Java – An Object Oriented Language CS 307 Lecture Notes Lecture Weeks 5-6 Khalid Siddiqui.
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];
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
COS 312 DAY 12 Tony Gauvin. Ch 1 -2 Agenda Questions? First Progress report due Assignment corrected 1 MIA – 2 A’s, 2 B’s, 1 D, 1 F and 1 MIA – Code\pig.
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.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
© 2004 Pearson Addison-Wesley. All rights reserved5-1 The if-else Statement An else clause can be added to an if statement to make an if-else statement.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
More on Arrays Review of Arrays of ints, doubles, chars
Lecture 5 array declaration and instantiation array reference
Arrays of Objects October 9, 2006 ComS 207: Programming I (in Java)
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Chapter VII: Arrays.
Parameter Lists & Command Line Arguments
Arrays, 2-D Arrays, and ArrayList
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
String Objects & its Methods
Presentation transcript:

ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects

ECE122 L13: Arrays of Objects March 15, 2007 Outline °Problem: How can I represent groups of objects in an array °Previously considered arrays of primitives °This can get complicated How many references are there to objects? °Arrays as parameters Arrays can be used as input and return values from methods

ECE122 L13: Arrays of Objects March 15, 2007 One dimensional Array °We have introduced one-dimensional array in our previous lectures. °An array is a collection of variables of the same type, referred by a common name. °type array-name [] = new type[size]; °int age[] = new int[5]; //declaration °age[0] = 20; //assignment

ECE122 L13: Arrays of Objects March 15, 2007 Definitions Using Array Literals °Used when exact size and initial values of an array are known in advance used to initialize the array handle int[] count = { 5,6,3,10 }; Visualize the results of the above command count[0] [1] [2] [3]

ECE122 L13: Arrays of Objects March 15, 2007 Contrast Definitions count[0] [1] [2] [3] 0 0 STUDENTS[0] [1] [2] [6] Bashful Doc... Sneezy Elements are primitive values ( 0s in this case) Elements are objects

ECE122 L13: Arrays of Objects March 15, 2007 Arrays of Objects °The elements of an array can be object references °The following declaration reserves space to store 5 references to String objects String[] words = new String[5]; °It does NOT create the String objects themselves °Initially an array of objects holds null references °Each object stored in an array must be instantiated separately

ECE122 L13: Arrays of Objects March 15, 2007 Arrays of Objects °The words array when initially declared: words °At this point, the following reference would throw a NullPointerException : System.out.println (words[0]);

ECE122 L13: Arrays of Objects March 15, 2007 Arrays of Objects °After some String objects are created and stored in the array: “friendship” words - - “loyalty” “honor”

ECE122 L13: Arrays of Objects March 15, 2007 Arrays of Objects °Keep in mind that String objects can be created using literals °Following declaration creates an array object called verbs Fills it with four String objects created using string literals String[] verbs = {"play", "work", "eat", "sleep"}; These are referenced by verbs[0] through verbs[3].

ECE122 L13: Arrays of Objects March 15, 2007 Definitions Using Array Literals °Create an array of strings String [] names = { "Bashful", "Doc”, "Sneezy"}; names[0] [1] [2] Bashful Doc Sneezy

ECE122 L13: Arrays of Objects March 15, 2007 Arrays as Objects °In Java, an array is an object. If the type of its elements is anyType, the type of the array object is anyType[ ]. °There are two ways to declare an array: anyType [ ] arrName; The difference becomes significant only when several variables are declared in one statement: int [ ] a, b; // both a, b are arrays int a [ ], b; // a is an array, b is not anyType arrName [ ]; or

ECE122 L13: Arrays of Objects March 15, 2007 Arrays as Objects °As with other objects, the declaration creates only a reference, initially set to null. An array must be created before it can be used. °There are two ways to create an array: arrName = new anyType [ length] ; Brackets, not parens! or arrName = new anyType [ ] { val1, val2, …, valN };

ECE122 L13: Arrays of Objects March 15, 2007 Array’s Length °The length of an array is determined when that array is created. °The length is either given explicitly or comes from the length of the {…} initialization list. °The length of an array arrName is referred to in the code as arrName.length. °length appears like a public field (not a method) in an array object.

ECE122 L13: Arrays of Objects March 15, 2007 Arrays as Parameters °An entire array can be passed as a parameter to a method °The reference to the array is passed, Makes the formal and actual parameters aliases of each other °Changing an array element within the method changes the original °An individual array element can be passed to a method as well, The type of the formal parameter is the same as the element type. In this case, the call is ‘by value.’

ECE122 L13: Arrays of Objects March 15, 2007 Passing to Methods °Example: /** * Swaps a [ i ] and a [ j ] */ public void swap (int a[ ], int i, int j) { int temp = a [ i ]; a [ i ] = a [ j ]; a [ j ] = temp; }

ECE122 L13: Arrays of Objects March 15, 2007 Returning Arrays from Methods °As for other objects, an array can be returned from a method. °The returned array is usually constructed within the method or obtained from calls to other methods. °The return type of a method that returns an array with someType elements is designated as someType [ ].

ECE122 L13: Arrays of Objects March 15, 2007 Returning from Methods °Example: public double[ ] solveQuadratic (double a, double b, double c) { double d = b * b - 4 * a * c; if (d < 0) return null; d = Math.sqrt(d); double roots[ ] = new double[2]; roots[0] = ( - b - d) / (2*a); roots[1] = ( - b + d) / (2*a); return roots; } Or simply: return new double[ ] { ( - b - d) / (2*a), ( - b + d) / (2*a) };

ECE122 L13: Arrays of Objects March 15, 2007 Command-Line Arguments °The signature of the main method indicates that it takes an array of String objects as a parameter °These values come from command-line arguments that are provided when the interpreter is invoked °For example, the following invocation of the interpreter passes three String objects into main : > java StateEval pennsylvania texas arizona °These strings are stored at indexes 0-2 of the array parameter of the main method

ECE122 L13: Arrays of Objects March 15, 2007 Summary °We can now make complicated data structures Objects still the basic units of data storage °Arrays are fundamental Most data is stored in arrays Allows for easy data access °Command line arguments are a good example of an array of strings °More to come: two dimensional arrays