Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 Arrays and the ArrayList Class Arrays of Objects.

Similar presentations


Presentation on theme: "Chapter 8 Arrays and the ArrayList Class Arrays of Objects."— Presentation transcript:

1 Chapter 8 Arrays and the ArrayList Class Arrays of Objects

2 2 Contents I. String Arrays II. Arrays of Objects III. Command-Line Arguments and Variable- Length Argument Lists

3 3 I. String Arrays 1. Creating String Arrays 2. Calling String Methods from an Array Element

4 4 I.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” };

5 5 I.1. Creating String Arrays 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

6 6 I.1. Creating String Arrays Creating an initialized array of String objects: String[] names = new String[3]; null address null The names variable holds the address of a String array The String array is an array of references to String objects

7 7 I.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”;

8 8 I.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. System.out.println(names[0].toUpperCase()); //Declare a char variable named letter char letter; //Assign the first character in names[3] to letter. Letter = names[3].charAt(0); An element of the names array

9 9 I.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());

10 10 Checkpoint 8.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.

11 11 II. 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. Each element of the array is initialized with the value null.

12 12 II. 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();

13 13 Checkpoint 8.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.

14 14 III. Command-Line Arguments and Variable-Length Argument Lists 1. Command-Line Arguments 2. Variable-Length Argument Lists

15 15 III.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.

16 16 III.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 Good morning

17 17 III.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) The ellipsis indicates that numbers is a vararg parameter. The vararg parameter can take a variable number of arguments. In fact, vararg parameters are actually arrays.

18 18


Download ppt "Chapter 8 Arrays and the ArrayList Class Arrays of Objects."

Similar presentations


Ads by Google