Can store many of the same kind of data together Chapter 9 Arrays Can store many of the same kind of data together Allows a collection of related values to be stored together with a single descriptive name A data structure with a fixed length Each item is called an element and each element has an index value. The friends array has 5 elements: In the diagram, Kermit is the first element and has index 0. The last and fifth element is Myah with index 4. © 2012 EMC Publishing, LLC
Chapter 9 Creating and Initializing Arrays 12/29/2018 4:37 AM Chapter 9 Creating and Initializing Arrays Declare and then allocate space: String[] friends; //declare array friends = new String[5]; //allocate space Declare and allocate: String[] friends = new String[5]; Declare, allocate, and initialize: String[] friends = {"Jo","Ed","Ty","Lu","KC"}; © 2012 EMC Publishing, LLC
Chapter 9 Accessing Array Elements 12/29/2018 4:37 AM Chapter 9 Accessing Array Elements An array element is accessed by including its index in brackets after the array name: System.out.println(friends[2]); An array element is changed through assignment: friends[2] = "Sunshine"; © 2012 EMC Publishing, LLC
Chapter 9 Traversing an Array 12/29/2018 4:37 AM Chapter 9 Traversing an Array The length attribute is used to determine the length of an array: numElements = friends.length; A for loop is one way to traverse an array: for (int i = 0; i < friends.length; i++) { System.out.println(friends[i]); } A for-each loop is also used to traverse an array: for (String element : friends) { System.out.println(element); } Note that the length attribute should not include parentheses after it. length is an attribute, not a method. Traversing an array with a for loop requires determining the length of the array. The loop iterates from 0 to one less than the length. The for-each loop does not require a loop control variable and helps prevent the exception ArrayIndexOutOfBoundsException. However, it cannot be used in situations where the array index value is needed. For example, when the elements of an array are to be listed in reverse order. © 2012 EMC Publishing, LLC
Chapter 9 Array Parameters A method declaration can include array parameters: public static void changeArray(int[] nums) Passing the whole array to a method passes the reference to the elements, allowing the actual array elements to be changed. A method declaration can include an array element: public static void useElement(int num) Passing just an element to a method passes a copy of the value, preventing the element in the array from being changed. © 2012 EMC Publishing, LLC
Chapter 9 Arrays with Meaningful Indexes 12/29/2018 4:37 AM Chapter 9 Arrays with Meaningful Indexes Use the index value of an array element for determining the storage location of a value Simplifies storage and retrieval of data For ranges of data that start at a high value, offset array indexes are used. When using offset array indexes, the index value is calculated with the formula: highValue – lowValue + 1 © 2012 EMC Publishing, LLC
Chapter 9 The String Class 12/29/2018 4:37 AM Chapter 9 The String Class A String object can be converted to an array of characters: toCharArray() An individual character of a String object can be converted to char value: charAt() © 2012 EMC Publishing, LLC
Chapter 9 Unicode and char 12/29/2018 4:37 AM Chapter 9 Unicode and char Uses a set of sixteen 1s and 0s to form a sixteen-bit binary code for a symbol. For example, the Unicode symbol for V is 00000000 01010110, which translates to 8610. When a letter is assigned to a char variable, the variable actually stores the Unicode representation of the letter. Because char is a primitive data type, char values can be compared with relational operators Type casting can be used to produce the Unicode equivalent character for a number © 2012 EMC Publishing, LLC
Simplest searching algorithm 12/29/2018 4:37 AM Chapter 9 Linear Search Simplest searching algorithm Checks each element of an array, one after the other, until a specified value has been found or until the entire array has been checked. © 2012 EMC Publishing, LLC
Chapter 9 Two-Dimensional Arrays 12/29/2018 4:37 AM Chapter 9 Two-Dimensional Arrays Represents data that corresponds to a grid An element is referred to by its row and column. The tttBoard[1][2] element stores an X: © 2012 EMC Publishing, LLC
Chapter 9 Two-Dimensional Arrays 12/29/2018 4:37 AM Chapter 9 Two-Dimensional Arrays Declaration includes the type followed by two sets of brackets ([][]) The number of rows is determined with a statement similar to: arrayName.length The number of columns is determined with a statement similar to: arrayName[0].length Nested for loops are often used to access the elements of a two-dimensional array © 2012 EMC Publishing, LLC
Chapter 9 The ArrayList Class 12/29/2018 4:37 AM Chapter 9 The ArrayList Class Part of the java.util package A class for implementing a collection of objects (primitive types cannot be stored) Used for implementing a dynamic array ArrayList methods include: add() remove() get() set() indexOf() size() indexOf() method compares its object parameter to each element of the array using the object's equals() method A collection is a group of related objects, or elements, that are stored together as a single unit. An array is a collection. The ArrayList class is a class for implementing an array. The ArrayList class implements a dynamic array. A dynamic array varies in size during run time and is used in applications where the size of an array may need to grow or shrink. Arrays are a fixed-length structure, but ArrayLists are dynamic. The indexOf() method compares objects. Therefore, relational operators cannot be used to determined how one object relates to another. The indexOf() method calls the object's equals() method to compare objects. Objects stored in an ArrayList must have an appropriately overridden equals() method. © 2012 EMC Publishing, LLC
Chapter 9 Wrapper Classes 12/29/2018 4:37 AM Chapter 9 Wrapper Classes Used to "wrap" primitive values in an object Integer and Double classes implement the Comparable interface Integer Class includes methods: compareTo() intValue() Double class includes methods: compareTo() doubleValue() © 2012 EMC Publishing, LLC