Chapter 8 Chapter 8 Arrays and the ArrayList Class Arrays of Objects 2 Contents I String Arrays II Arrays of Objects III Command Line Arguments and Variable Length Argument Lists 3 I String Arrays 1 C.
Trang 1Chapter 8
Arrays and the
ArrayList Class
Arrays of Objects
Trang 2I String Arrays
II Arrays of Objects
III Command-Line Arguments and
Variable-Length Argument Lists
Trang 3I String Arrays
1 Creating String Arrays
2 Calling String Methods from an Array
Element
Trang 4I.1 Creating String Arrays
In memory, an array of String objects is
arranged differently than an array of a primitive
data type
An array of String objects is really an array of references to String objects
Creating an array of String objects initialized
with values:
String[] names = {“Bill”, “Susan”, “Steven” };
Trang 5I.1 Creating String Arrays
address address
“Bill” “Susan” “Steven”
The names variable holds the
address of a String array
The String array is
an array of references
to String objects
Trang 6I.1 Creating String Arrays
Creating an initialized array of String objects:
String[] names = new String[3];
null null
The names variable holds the address of a String array
The String array is
an array of references
to String objects
Trang 7I.1 Creating String Arrays
When we create an initialized array of String
objects, we must assign a value to each element
in the array that we intend to use
String[] names = new String[3];
names[0] = “Bill”;
names[1] = “Susan”;
names[2] = “Steven”;
Trang 8I.2 Calling String Methods from
an Array Element
Each element of a String array is a String
object, we can use an element to call a String
method
//Declare a char variable named letter
char letter;
//Assign the first character in names[3] to letter.
An element of the names array
Trang 9I.2 Calling String Methods from
an Array Element
The following loop displays the length of each
string held in names:
for(int i = 0; i < names.length; i++)
System.out.println(names[i].length());
Trang 108.15
a Write a statement that declares a String array initialized with the following strings: “Mercury”,
“Venus”, “Earth”, and “Mars”
b Write a loop that displays the contents of each element in the array you declared in a
c Write a loop that displays the first character of the strings stored in each element of the array you declared in a
Trang 11II Arrays of Objects
Like any other data type, we can create arrays of class objects
BankAccount[] accounts = new BankAccount[5];
The variable that references the array is named accounts
As with String arrays, each element in this
array is a reference variable
Trang 12II Arrays of Objects
We must individually create the objects that each element will reference
for(int index = 0; index < accounts.length; index++)
accounts[index] = new BankAccount();
Trang 138.16 Write a Rectangle class Write code that declares a Rectangle array with five elements Instantiate each element with a Rectangle
object Use constructor to initialize each object
with values for the length and width fields
Trang 14III Command-Line Arguments and Variable-Length Argument Lists
1 Command-Line Arguments
2 Variable-Length Argument Lists
Trang 15III.1 Command-Line Arguments
Consider the main method's header:
public static void main(String[] args)
The parameter args is an reference to an arry of String
The array that is passed into the args parameter comes from the operating system command-line
Trang 16III.1 Command-Line Arguments
/**
This program displays the arguments passed to it from the operating system command line.
*/
public class CommandLine
{
public static void main(String[] args)
{
for(int index; index < args.length; index++)
System.out.println(args[index]);
}
}
Compile and the execute with the following command:
java CommandLine Good morning
Trang 17III.2 Variable-Length Argument
Lists
Java provides a mechanism known as
variable-length argument lists.
We can write a method that takes a variable
number of arguments
The method that accepts any number of
arguments when it is called
The parameter is known as a vararg parameter.
int sum( int numbers )