© 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and...
-
Upload
juliet-knight -
Category
Documents
-
view
213 -
download
0
Transcript of © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and...
![Page 1: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/1.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-1
Chapter 1
Java Fundamentals -
Arrays and References (updated by Dan Fleck)
![Page 2: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/2.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-2
Arrays
• Collection of elements with the same data type• Array elements have an order• Support direct and random access• One-dimensional arrays
– Declaration examplefinal 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
![Page 3: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/3.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-3
Arrays
Figure 1-7Figure 1-7One-dimensional array of at most seven elements
![Page 4: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/4.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-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 examplefinal int DAYS_PER_WEEK = 7;
final int WEEKS_PER_YEAR = 52;
double[][] minTemps = new double[DAYS_PER_WEEK][WEEKS_PER_YEAR];
![Page 5: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/5.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-5
Arrays
Figure 1-8Figure 1-8A two-dimensional array
![Page 6: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/6.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-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
![Page 7: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/7.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-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
typedouble radius; // radius of a sphere
String name; // reference to a String object
![Page 8: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/8.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-8
Primitive Data Types
Figure 1-5Figure 1-5Primitive data types and corresponding wrapper classes
![Page 9: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/9.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-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
![Page 10: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/10.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-10
References
int myVar = 23;MyObject myObj = new MyObject(12, “I was here”);
myVar 23
Memory
myObj 0x121AB450
0x121AB450 12
I was here0x121AB470
.
.
.
.
<--primitive
<--reference
![Page 11: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/11.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-11
References
myVar 23
Memory
myObj 0x121AB450
0x121AB450 12
I was here0x121AB470
.
.
.
.
<--primitive (literal)
<--reference
method(myVar);
public void method(int x) { x = 45;}
myVar = ?
Really passes in 23
x 23
.
![Page 12: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/12.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-12
References
myVar 23
Memory
myObj 0x121AB450
0x121AB450 12
I was here0x121AB470
.
.
.
.
<--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
.
![Page 13: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/13.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-13
References
myVar 23
Memory
myObj 0x121AB450
0x121AB450 12
I was here0x121AB470
.
.
.
.
<--primitive (literal)
<--reference
int otherVar = myVar;otherVar = 67;
myVar = ?
23otherVar
23
67
67
![Page 14: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/14.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-14
References
myVar 23
Memory
myObj 0x121AB450
0x121AB450 12
I was here0x121AB470
.
.
.
.
<--primitive (literal)
<--reference
MyObject otherObj = myObj;otherObj.myNumber = 276;
myObj.myNumber = ?
0x121AB450otherObj
276
276
![Page 15: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/15.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-15
References
myVar 23
Memory
myObj 0x121AB450
0x121AB450 12
I was here0x121AB470
.
.
.
.
MyObject otherObj = myObj;
0x121AB450otherObj
276
276I 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
![Page 16: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/16.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-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);
![Page 17: © 2006 Pearson Addison-Wesley. All rights reserved1-1 Chapter 1 Java Fundamentals - Arrays and References (updated by Dan Fleck)](https://reader036.fdocuments.us/reader036/viewer/2022082821/5697bfad1a28abf838c9c3b7/html5/thumbnails/17.jpg)
© 2006 Pearson Addison-Wesley. All rights reserved 1-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”;