1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses.

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
Arrays.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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 One-Dimensional (1-D) Array Overview l Why do we need 1-D array l 1-D array declaration and Initialization l Accessing elements of a 1-D array l Passing.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
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”
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 6: Arrays Java Software Solutions Third Edition
Unit 5 1 Arrays and Vectors H Arrays and vectors are objects that help us organize large amounts of information H We will talk about: array declaration.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
 2006 Pearson Education, Inc. All rights reserved Arrays.
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 Dr. Seuss again: "Too Many Daves"  Did I ever tell you that Mrs. McCave Had twenty-three sons, and she named them all Dave?  Well, she did. And that.
Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science by John Lewis, William Loftus, and Cara Cocking Java Software.
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.
String Processing Opening Discussion zDo you have any questions about the quiz? zWhat did we talk about last class? zDo you have questions.
© 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.
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.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
Programming in Java (COP 2250) Lecture Chengyong Yang Fall, 2005.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
1 Lecture 6 Objects for Organizing Data Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
1 Sorting b Sorting is the process of arranging a list of items into a particular order b There must be some value on which the order is based b There.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Java Software Solutions Lewis and Loftus Chapter 3 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Program Elements -- Introduction.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
1 Enhanced Class Design Introduction zWe now examine several features of class design and organization that can improve reusability and system elegance.
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.
Arrays. Arrays are objects that help us organize large amounts of information.
© 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.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 6: Arrays Presentation slides for Java Software Solutions for AP* Computer Science 3rd Edition.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
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.
CSC 211 Java I for loops and arrays.
Lecture 6 Objects for Organizing Data
Two-Dimensional Arrays
Computer Programming BCT 1113
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.
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
Arrays in Java.
Visual Programming COMP-315
Arrays of Objects October 8, 2007 ComS 207: Programming I (in Java)
Arrays.
Presentation transcript:

1 Objects for Organizing Data -- Introduction zAs our programs get more sophisticated, we need assistance organizing large amounts of data zChapter 6 focuses on: yarray declaration and use yarrays of objects yparameters and arrays ymultidimensional arrays  the Vector class yadditional techniques for managing strings

2 Arrays zAn array is an ordered list of values zEach value has a numeric index zAn array of size N is indexed from zero to N-1 zThe following array of integers has a size of 10 and is indexed from 0 to scores

3 Arrays zA particular value in an array is referenced using the array name followed by the index in brackets zFor example, the expression scores[4] refers to the value 67 (which is the 5th value in the array) zThat expression represents a place to store a single integer, can can be used wherever an integer variable can zFor example, it can be assigned a value, printed, used in a calculation

4 Arrays zAn array stores multiple values of the same type zThat type can be primitive types or objects zTherefore, we can create an array of integers, or an array of characters, or an array of String objects, etc. zIn Java, the array itself is an object zTherefore the name of the array is a object reference variable, and the array itself is instantiated separately

5 Declaring Arrays  The scores array could be declared as follows: int[] scores = new int[10]; zNote that the type of the array does not specify its size, but each object of that type has a specific size  The type of the variable scores is int[] (an array of integers) zIt is set to a newly instantiated array of 10 integers  See Basic_Array.java

6 Declaring Arrays zSome examples of array declarations: float[] prices = new float[500]; boolean[] flags; flags = new boolean[20]; char[] codes = new char[1750];

7 Bounds Checking zOnce an array is created, it has a fixed size zAn index used in an array reference must specify a valid element zThat is, they must be in bounds (0 to N-1) zThe Java interpreter will throw an exception if an array index is out of bounds zThis is called automatic bounds checking zIts common to inadvertently introduce off-by- one errors when using arrays

8 Bounds Checking  Each array object has a public constant called length that stores the size of the array zIt is referenced through the array name (just like any other object): scores.length  Note that length holds the number of elements, not the largest index  See Reverse_Numbers.java and Adjust_Test_Scores.java

9 Array Declarations Revisited zThe brackets of the array type can be associated with the element type or with the name of the array zTherefore float[] prices; and float prices[]; are essentially equivalent zThe first format is usually more readable

10 Initializer Lists zAn initializer list can be used to instantiate and initialize an array in one step zThe values are delimited by braces and separated by commas zExamples: int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298,476}; char[] letter_grades = {'A', 'B', 'C', 'D', 'F'};

11 Initializer Lists zNote that when an initializer list is used:  the new operator is not used yno size value is specified zThe size of the array is determined by the number of items in the initializer list zAn initializer list can only be used in the declaration of an array  See Primes.java and Sales_Analysis.java

12 Arrays of Objects zThe elements of an array can be object references zThe declaration String[] words = new String[25]; reserves space to store 25 references to String objects  It does NOT create the String objects themselves zEach object stored in an array must be instantiated separately

13 Arrays of Objects  See Children.java and Presidents.java zObjects can have arrays as instance variables zTherefore, fairly complex structures can be created simply with arrays and objects zThe software designer must carefully determine an organization of data and objects that makes sense for the situation  See Roll_Call.java

14 Arrays as Parameters zAn entire array can be passed to a method as a parameter zLike any other object, the reference to the array is passed, making the formal and actual parameters aliases of each other zChanging an array element in the method changes the original zAn array element can be passed to a method as well, and follow the parameter passing rules of that element's type  See Array_Test.java

15 Multidimensional Arrays zA one-dimensional array stores a simple list of values zA two-dimensional array can be thought of as a table of values, with rows and columns zA two-dimensional array element is referenced using two index values zTo be precise, a two-dimensional array in Java is an array of arrays, therefore each row can have a different length

16 Multidimensional Arrays zAn initializer list can be used to create and set up a multidimensional array zEach element in the list is itself an initializer list  Note that each array dimension has its own length constant  See Multi_Array_Test.java and Soda_Survey.java

17 The Vector Class  An object of class Vector is similar to an array in that it stores multiple values zHowever, a vector yonly stores objects ydoes not have the indexing syntax that arrays have zService methods are used to interact with a vector  The Vector class is part of the java.util package  See Beatles.java and ZZ_Top.java

18 The Vector Class zAn important difference between an array and a vector is that a vector can be thought of as a dynamic, able to change its size as needed zEach vector initially has a certain amount of memory space reserved for storing elements zIf an element is added that doesn't fit in the existing space, more room is automatically acquired

19 The Vector Class zA vector is implemented using an array zWhenever new space is required, a new, larger array is created, and the values are copied from the original to the new array zTo insert an element, existing elements are first copied, one by one, to another position in the array  Therefore, the implementation of Vector in the API is not very efficient

20 The StringTokenizer Class Revisited  We've seen a StringTokenizer object separate a string into separate tokens zBy default, those tokens are delimited by white space  But by using other StringTokenizer constructors, we can define the delimiters used to define a token zWe can also set whether we want the delimiters themselves returned as tokens  See Voltaire.java and URL_Tokens.java

21 The StringBuffer Class  Recall that the value of a String object is immutable; once set it cannot be changed  The StringBuffer class can be used to define a character string whose value can change zIt's service methods include the ability to append and insert characters  See Money.java  However, most functionality defined by the StringBuffer class can be accomplished with String objects and string concatenation