© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

COSC 2006 Data Structures I Instructor: S. Xu URL:
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
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,
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 Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Introduction to arrays Data in economy size packages.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
ECE122 L13: Arrays of Objects March 15, 2007 ECE 122 Engineering Problem Solving with Java Lecture 13 Arrays of Objects.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
ISBN Chapter 6 Data Types. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.6-2 Figure 6.1 IEEE floating-point formats: (a)
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
CS0007: Introduction to Computer Programming Introduction to Arrays.
More Arrays Length, constants, and arrays of arrays By Greg Butler.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Java Unit 9: Arrays Declaring and Processing Arrays.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 9: Advanced Array Concepts
C Static Arrays Pepper. What is an array? Memory locations – same type – next to each other (contiguous) – Same name – Indexed by position number of type.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Introduction to Collections Arrays. Collections Collections allow us to treat a group of values as one collective entity. The array is a collection of.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
© 2004 Pearson Addison-Wesley. All rights reserved October 10, 2007 Parameter Lists & Command Line Arguments ComS 207: Programming I (in Java) Iowa State.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Objects and Classes.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Copyright © 2006 Pearson Addison-Wesley. All rights reserved This Weeks Topics: Pointers (continued)  Modify C-String through a function call 
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter Array Basics.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
IT259 Foundation of Programming Using Java Unit 9 Seminar : (Chapter 8 ) Instructor : Vladimir Gubanov, PhD
1. Define an array 1 Create reference arrays of objects in Java program 2 Initialize elements of arrays 3 Pass array to methods 4 Return array to methods.
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.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Two Dimensional Arrays Found in chapter 8, Section 8.9.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved7-1 Array review Array of primitives int [] count; count = new int[10]; Array of objects Grade [] cs239;
 2005 Pearson Education, Inc. All rights reserved Arrays.
(C) 2010 Pearson Education, Inc. All rights reserved.  Best way to develop and maintain a large program is to construct it from small, simple pieces,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Outline Creating Objects The String Class The Random and Math Classes
(C) 2010 Pearson Education, Inc. All rights reserved.
Review for Midterm 3.
Corresponds with Chapter 5
Review of Java Fundamentals
Presentation transcript:

© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)

© 2006 Pearson Addison-Wesley. All rights reserved1-2 Arrays Collection of elements with the same data type Array elements have an order Support direct and random access One-dimensional arrays –Declaration example final int DAYS_PER_WEEK = 7; double [] maxTemps = new double[DAYS_PER_WEEK]; –Length of an array is accessible using data field length –Use an index or subscript to access an array element

© 2006 Pearson Addison-Wesley. All rights reserved1-3 Arrays Figure 1-7 One-dimensional array of at most seven elements

© 2006 Pearson Addison-Wesley. All rights reserved1-4 Arrays One-dimensional arrays (continued) –Initializer list example double [] weekDayTemps = {82.0, 71.5, 61.8, 75.0, 88.3}; –You can also declare array of object references Multidimensional arrays –Use more than one index –Declaration example final int DAYS_PER_WEEK = 7; final int WEEKS_PER_YEAR = 52; double[][] minTemps = new double[DAYS_PER_WEEK][WEEKS_PER_YEAR];

© 2006 Pearson Addison-Wesley. All rights reserved1-5 Arrays Figure 1-8 A two-dimensional array

© 2006 Pearson Addison-Wesley. All rights reserved1-6 Arrays Passing an array to a method –Declare the method as follows: public double averageTemp(double[] temps, int n) –Invoke the method by writing: double avg = averageTemp(maxTemps, 6); –Location of array is passed to the method Cannot return a new array through this value –Method can modify content of the array

© 2006 Pearson Addison-Wesley. All rights reserved1-7 Variables Represents a memory location Contains a value of primitive type or a reference Its name is a Java identifier Declared by preceding variable name with data type double radius; // radius of a sphere String name; // reference to a String object

© 2006 Pearson Addison-Wesley. All rights reserved1-8 Primitive Data Types Figure 1-5 Primitive data types and corresponding wrapper classes

© 2006 Pearson Addison-Wesley. All rights reserved1-9 References Data type used to locate an object Java does not allow programmer to perform operations on the reference value Location of object in memory can be assigned to a reference variable

© 2006 Pearson Addison-Wesley. All rights reserved1-10 References int myVar = 23; MyObject myObj = new MyObject(12, “I was here”); myVar 23 Memory myObj 0x121AB I was here 0x121AB <--primitive <--reference

© 2006 Pearson Addison-Wesley. All rights reserved1-11 References myVar 23 Memory myObj 0x121AB I was here 0x121AB <--primitive (literal) <--reference method(myVar); public void method(int x) { x = 45; } myVar = ? Really passes in 23 x 23.

© 2006 Pearson Addison-Wesley. All rights reserved1-12 References myVar 23 Memory myObj 0x121AB I was here 0x121AB <--primitive (literal) <--reference method(myObj); public void method(MyObject x) { x.myNumber = 45; x.myString = “Was I here?”; } myObj = ? myObj.myNumber = ? myObj.myString = ? Really passes in 0x121AB450 x 0x121AB450.

© 2006 Pearson Addison-Wesley. All rights reserved1-13 References myVar 23 Memory myObj 0x121AB I was here 0x121AB <--primitive (literal) <--reference int otherVar = myVar; otherVar = 67; myVar = ? 23 otherVar 23 67

© 2006 Pearson Addison-Wesley. All rights reserved1-14 References myVar 23 Memory myObj 0x121AB I was here 0x121AB <--primitive (literal) <--reference MyObject otherObj = myObj; otherObj.myNumber = 276; myObj.myNumber = ? 0x121AB450 otherObj 276

© 2006 Pearson Addison-Wesley. All rights reserved1-15 References myVar 23 Memory myObj 0x121AB I was here 0x121AB MyObject otherObj = myObj; 0x121AB450 otherObj I was here myVar otherObj Balloon analogy: Every object is a balloon Every reference is a piece of string Every variable can hold on to a piece of String

© 2006 Pearson Addison-Wesley. All rights reserved1-16 References - Questions How do I copy an object so I end up with two distinct instances? What do I do if I want a method to modify the value of an int? for example: divideByTwo(int number) ? What do I do if I want a method to modify an array? For example: sortArray(int [] myArray) ? Can I modify the values in an object like this: addFortySeven(myObject.myInt);

© 2006 Pearson Addison-Wesley. All rights reserved1-17 Named Constants Have values that do not change Declared as a variable but using the keyword final final static int MAX_VALUE=2345; final static String DAY1 = “Monday”;