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.
Chapter Arrays and the ArrayList Class Arrays of Objects Contents I String Arrays II Arrays of Objects III Command-Line Arguments and VariableLength Argument Lists I String Arrays Creating String Arrays Calling String Methods from an Array Element 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” }; I.1 Creating String Arrays The String array is an array of references to String objects The names variable holds the address of a String array address address “Bill” address “Susan” address “Steven” I.1 Creating String Arrays Creating an initialized array of String objects: String[] names = new String[3]; The String array is an array of references to String objects The names variable holds the address of a String array address null null null 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”; 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()); An element of the names array //Declare a char variable named letter char letter; //Assign the first character in names[3] to letter Letter = names[3].charAt(0); 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()); 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 10 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 11 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(); 12 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 13 III Command-Line Arguments and Variable-Length Argument Lists Command-Line Arguments Variable-Length Argument Lists 14 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 15 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 16 III.2 Variable-Length Argument Lists Java provides a mechanism known as variablelength 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 17 18 ... the length and width fields 13 III Command-Line Arguments and Variable-Length Argument Lists Command-Line Arguments Variable-Length Argument Lists 14 III.1 Command-Line Arguments Consider the. .. displays the first character of the strings stored in each element of the array you declared in a 10 II Arrays of Objects Like any other data type, we can create arrays of class objects BankAccount[]...Contents I String Arrays II Arrays of Objects III Command-Line Arguments and VariableLength Argument Lists I String Arrays Creating String Arrays Calling String Methods from