10-arrays java
TRANSCRIPT
![Page 1: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/1.jpg)
1
![Page 2: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/2.jpg)
2
Java Data Types
![Page 3: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/3.jpg)
3
Java Data Types
Try expressing these definitions in words
![Page 4: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/4.jpg)
4
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 5: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/5.jpg)
5
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 6: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/6.jpg)
6
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 7: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/7.jpg)
7
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 8: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/8.jpg)
8
One-Dimensional Arrays
Declare
Instantiate
![Page 9: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/9.jpg)
9
One-Dimensional Arrays
int[] numbers = new int[4];
Whattype ofvaluescan be
stored ineach cell
?
![Page 10: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/10.jpg)
10
One-Dimensional Arrays
float[] realNumbers = new float[10];
How do you
getvaluesinto the
cells?
![Page 11: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/11.jpg)
11
One-Dimensional Arrays
Array Initializersint[] numbers = {4.93, -15.2, 0.5, 1.67};
Initializersdo the
instantiationand
storing inwith the
declaration
![Page 12: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/12.jpg)
12
One-Dimensional Arrays
Accessing Individual Components
Indexing expression
![Page 13: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/13.jpg)
13
One-Dimensional Arrays
Whathappens
if youtry to
accessvalue[1000]
?
![Page 14: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/14.jpg)
14
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 15: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/15.jpg)
15
Two-Dimensional Arrays
Two-dimensional
arrayscan beused to
representtables
such asthis map
![Page 16: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/16.jpg)
16
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 17: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/17.jpg)
17
Two-Dimensional Arrays
![Page 18: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/18.jpg)
18
Two-Dimensional Arrays
![Page 19: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/19.jpg)
19
Two-Dimensional Arrays
Can you predict how each item is accessed?
![Page 20: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/20.jpg)
20
Two-Dimensional Arrays
![Page 21: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/21.jpg)
21
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 22: 10-Arrays JAVA](https://reader034.vdocuments.mx/reader034/viewer/2022052118/5580d9a8d8b42a03198b4cec/html5/thumbnails/22.jpg)
22
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