Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data...
-
date post
19-Dec-2015 -
Category
Documents
-
view
227 -
download
3
Transcript of Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data...
![Page 1: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/1.jpg)
Chapter 9
Arrays
![Page 2: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/2.jpg)
2
Knowledge Goals
• Understand the difference between atomic and composite data types
• Understand the difference between unstructured and structured composite data types
• Know how Java implements arrays• Know how an array is passed as an argument
![Page 3: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/3.jpg)
3
Knowledge Goals
• Understand the difference between an array and the information stored or referenced within the array
• Understand the role of an array in structuring data within a problem
• Understand the role of a two-dimensional array in representing a table with rows and columns
• Know how a two-dimensional array is constructed as an array of arrays
![Page 4: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/4.jpg)
4
Skill Goals
• Declare and instantiate a one-dimensional array
• Access and manipulate the individual components in a one-dimensional array where the elements are• Atomic types• Composite types
• Use an initializer list to instantiate a one-dimensional array
![Page 5: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/5.jpg)
5
Skill Goals
• Declare a two-dimensional array• Perform fundamental operations on a two-
dimensional array:• Access a component of the array• Process the array by rows• Process the array by columns
• Declare a two-dimensional array as a parameter
• Declare and process a multidimensional array
![Page 6: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/6.jpg)
6
Java Data Types
![Page 7: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/7.jpg)
7
Java Data Types
Try expressing these definitions in words
![Page 8: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/8.jpg)
8
Java Data Types
Composite data type
A data type that allows a collection of values to be associated with an identifier of that type
Unstructured data type
A collection of components that are not organized with respect to one another
Structured data type
An organized collection of components; the organization determines the means used to access individual components
Is a class structured?
![Page 9: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/9.jpg)
9
Java Data Types
class Example
{
int field1;
int field2;
double field3;
}
class Example
{
double field3;
int field2;
int field1;
}
Is a class structured? Did you change your answer?
Changing the order does not change the access
![Page 10: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/10.jpg)
10
One-Dimensional Arrays
Data structure
The implementation of a composite data type
Note the difference between a data structure (implementation of any composite type) and a structured data type (a composite type that is structured)
![Page 11: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/11.jpg)
11
One-Dimensional Arrays
One-dimensional array
A structured collection of components, all of the same type, that is given a single name; each component is accessed by an index that indicates the component's position within the collection
Class
composite, unstructured
heterogeneous
access by field name
Array
composite, structured
homogeneous
access by position
![Page 12: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/12.jpg)
12
One-Dimensional Arrays
Declare
Instantiate
![Page 13: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/13.jpg)
13
One-Dimensional Arrays
int[] numbers = new int[4];
Whattype ofvaluescan be
stored ineach cell
?
![Page 14: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/14.jpg)
14
One-Dimensional Arrays
float[] realNumbers = new float[10];
How do you
getvaluesinto the
cells?
![Page 15: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/15.jpg)
15
One-Dimensional Arrays
Array Initializersint[] numbers = {4.93, -15.2, 0.5, 1.67};
Initializersdo the
instantiationand
storing inwith the
declaration
![Page 16: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/16.jpg)
16
One-Dimensional Arrays
Accessing Individual Components
Indexing expression
![Page 17: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/17.jpg)
17
One-Dimensional Arrays
IndexingExpression
IndexingExpression
Place into whicha value is stored;value is changed
Place from whicha value is extracted;value is not changed
![Page 18: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/18.jpg)
18
One-Dimensional Arrays
Whathappens
if youtry to
accessvalue[1000]
?
![Page 19: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/19.jpg)
19
One-Dimensional Arrays
Out-of-bounds array index
An index that is either less than 0 or greater than the array size minus 1, causing an ArrayIndexoutOfBoundsException to be thrown
Length
A public instance variable associated with each instantiated array, accessed by array name .length
Use length to avoid out-of-bounds indexes
![Page 20: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/20.jpg)
20
One-Dimensional Arrays
Aggregate Array Operations
What does the following expression return?
numbers == values
![Page 21: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/21.jpg)
21
One-Dimensional Arrays
Now, what does the following expression return?
numbers == values
![Page 22: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/22.jpg)
22
One-Dimensional Arrays
System provides two useful array methods
first = second.clone(); // duplicates second import java.util.Arrays;
Arrays.equals(first, second); // item-by-item check
System.out.println(first == second);
System.out.println(Arrays.equals(first, second);
What is printed?
![Page 23: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/23.jpg)
23
More Examples
What does this code segment do?
totalOccupants = 0;
for (int aptNo = 0; aptNo < occupants.length; aptNo++)
totalOccupants = totalOccupants +occupants[aptNo];
![Page 24: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/24.jpg)
24
More Examples
if ((letter >= 'A' && letter <= 'Z' || letter >= 'a' && letter <= 'z')){ index = (int)Character.toUpperCase(letter) - (int)'A'; lettrCount[index] = letterCount[index] + 1;}
What doesthis codefragment
do?
![Page 25: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/25.jpg)
25
Arrays of Objects
String[] groceryItems = new String[10];
for (index = 0; index < grocerItems.length; index++)
{
groceryItems[index] = inFile.nextLine();
}
![Page 26: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/26.jpg)
26
Arrays of Objects
Expression Class/TypegroceryItems Reference to an array
groceryItems[0] Reference to a string
groceryItems[0].charAt() A character
groceryItems[10] Error
Base address
The memory address of the first element of the array
"Reference to" is the base address
![Page 27: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/27.jpg)
27
Arrays of Objects
How do Array.equals and clone operate with arrays of objects?
Array.equals uses the == operator to compare arrays, so the addresses are compared
clone uses the = operator, so the addresses are copied
Array.equals(numbers, numbers.clone()) ?
![Page 28: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/28.jpg)
28
Date[] bigEvents = new Date[10];
Arrays of Objects
[ 0 ]
[ 1 ] .
.
.
[ 9 ]
bigEvents Expression Class/Type
bigEvents Array
bigEvents[0] Date
bigEvents[0].month String
bigEvents[0].day int
bigEvents[0].year int
bigEvents[0].month.charAt(0) char
Date. ..
Date. ..
Date. ..
![Page 29: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/29.jpg)
29
Arrays of Objects
length is the number of slots assigned to the array
What if the array doesn’t have valid data in each of these slots?
Keep a counter of how many slots have valid data and use this counter when processing the array
More about this type of processing in Chapter 11
![Page 30: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/30.jpg)
30
Arrays of Objects
The Vector class
A class available in java.util that offers functionality similar to that of a one-dimensional array of objects
Objects of class Vector can grow and shrink; the size is not fixed for its lifetime
However, internal resizing operations are time consuming
What do you think is the Vector class data structure?
![Page 31: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/31.jpg)
31
public static double average(int[] grades)
// Calculates and returns the average grade in an
// array of grades.
// Assumption: All array slots have valid data.
{
int total = 0;
for (int i = 0; i < grades.length; i++)
total = total + grades[i];
return (double) total / (double) grades.length;
}
Arrays of Objects
What is passed as an argument?
![Page 32: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/32.jpg)
32
Two-Dimensional Arrays
Two-dimensional
arrayscan beused to
representtables
such asthis map
![Page 33: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/33.jpg)
33
Two-Dimensional Arrays
Two-dimensional array
A collection of homogeneous components,
structured in two dimensions (referred to as
rows and columns); each component is
accessed by a pair of indexes representing
the component’s position within each
dimension
![Page 34: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/34.jpg)
34
Two-Dimensional Arrays
![Page 35: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/35.jpg)
35
Two-Dimensional Arrays
![Page 36: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/36.jpg)
36
Two-Dimensional Arrays
Can you predict how each item is accessed?
![Page 37: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/37.jpg)
37
Two-Dimensional Arrays
![Page 38: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/38.jpg)
38
Two-Dimensional Arrays
Actual Java implementation
![Page 39: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/39.jpg)
39
Two-Dimensional Arrays
hiTemp.length is the number of rows
hiTemp[2].length is the number of columns in row two
![Page 40: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/40.jpg)
40
Two-Dimensional Arrays
int[][] hiTemp = new int[6][12];
for (int col = 0; col < hiTemp[2].length; col++)
System.out.println(data[2][col]);
What is printed?
![Page 41: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/41.jpg)
41
Two-Dimensional Arrays
Subarray processing by row
![Page 42: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/42.jpg)
42
Two-Dimensional Arrays
for (int row = 0; row < rowsFilled; row++)
{
// Array is not ragged
total = 0;
for (int col = 0; col < colsFilled; col++)
total = total + data[row][col];
outFile.println("Row sum: " + total);}
![Page 43: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/43.jpg)
43
Two-Dimensional Arrays
Subarray processing by column
![Page 44: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/44.jpg)
44
Two-Dimensional Arrays
for (int col = 0; col < colsFilled; col++)
{
// Array is not ragged
total = 0;
for (int row = 0; row < rowsFilled; row++)
total = total + data[row][col];
outFile.println("Column sum: " + total);}
![Page 45: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/45.jpg)
45
Two-Dimensional Arrays
When processing by row,
the outer loop is ______ (row, column)?
the inner loop is ______ (row, column)?
When processing by column,
the outer loop is ______ (row, column)?
the inner loop is ______ (row, column)?
![Page 46: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/46.jpg)
46
int[][] hits = {{ 2, 1, 0, 3, 2 },
{ 1, 1, 2, 3 },
{ 1, 0, 0, 0, 0 },
{ 0, 1, 2, 1, 1 }};
[0] [1] [2] [3] [4]
Two-Dimensional Arrays
hits
Initializer Lists
2 1 0 3 2
1 1 2 3
1 0 0 0 0
0 1 2 1 1
![Page 47: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/47.jpg)
47
Three-Dimensional Arrays
Array
A collection of homogeneous components ordered on N dimensions (N>=1); each component is accessed by N indexes, each of which represents the component's position within that dimension
![Page 48: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/48.jpg)
48
Extras
I loved togive talks to
young people; I used colored
wires todemonstrate
differentcomputer
speeds
Who am I?
![Page 49: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/49.jpg)
49
Extras - GUI Track
Rather than using JOptionPane, you can build your own window using class JFrame
JFrame outFrame = new JFrame();
outFrame consists of the outside of the window frame that holds controls and the content pane which shows the content
![Page 50: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/50.jpg)
50
Extras - GUI Track
![Page 51: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/51.jpg)
51
Extras - GUI Track
Handling your own window with JFrame requires
declaring and instantiating a JFrame object setting the size of the object declaring and getting a pane setting the layout manager for the pane adding text to the pane making the frame visible
![Page 52: Chapter 9 Arrays. 2 Knowledge Goals Understand the difference between atomic and composite data types Understand the difference between unstructured and.](https://reader036.fdocuments.in/reader036/viewer/2022062308/56649d375503460f94a1047f/html5/thumbnails/52.jpg)
52
Extras - GUI Track
JFrame objectwith a
text field