EO Gaddis Java Chapter 07 6e
EO Gaddis Java Chapter 07 6e
Arrays and
the
ArrayList
Class
0 0 0 0 0 0
index 0 index 1 index 2 index 3 index 4 index 5
2 4 6 8 10
array1 holds an
Address
address to the array
Example:
array2 holds an
SameArray.java
Address
address to the array
showArray(numbers); 5 10 15 20 25 30 35 40
outputFile.close();
Address
names[0] null
names[1] null
names[2] null
names[3] null
names[0] = "Bill";
The names variable holds names[1] = "Susan";
the address to the array. names[2] = "Steven";
names[3] = "Jean";
Address
System.out.println(names[0].toUpperCase());
char letter = names[3].charAt(0);
Address
inventory[0] null
inventory[1] null
inventory[2] null
inventory[3] null
inventory[4] null
Copyright © 2018 Pearson Education, Inc. All Rights Reserved
Arrays of Objects
• Each element needs to be initialized.
for (int i = 0; i < inventory.length; i++)
inventory[i] = new InventoryItem();
• Example: ObjectArray.java
description: “”
The inventory variable holds the address units: 0
of an InventoryItem array.
description: “”
Address units: 0
description: “”
inventory[0] Address units: 0
inventory[1] Address description: “”
inventory[2] Address units: 0
row 0
row 1
row 2
row 3
• Example: ArrayListDemo1.java
See: ArrayListDemo6.java
• Example: CommandLine.java
• It is not required that the name of main’s
parameter array be args.