Presentation is loading. Please wait.

Presentation is loading. Please wait.

Can store many of the same kind of data together

Similar presentations


Presentation on theme: "Can store many of the same kind of data together"— Presentation transcript:

1 Can store many of the same kind of data together
Chapter 10 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: Refer to page 239 in the text. In the diagram, Kermit is the first element and has index 0. The last and fifth element is Myah with index 4. © 2007 Lawrenceville Press

2 Chapter 10 Creating and Initializing Arrays
11/23/2018 5:18 AM Chapter 10 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"}; Refer to pages 239 and 240 in the text. © 2007 Lawrenceville Press

3 Chapter 10 Accessing Array Elements
11/23/2018 5:18 AM Chapter 10 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"; Refer to page 240 in the text. © 2007 Lawrenceville Press

4 Chapter 10 Traversing an Array
11/23/2018 5:18 AM Chapter 10 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); } Refer to pages 240 and 241 in the text. 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. © 2007 Lawrenceville Press

5 Chapter 10 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. Refer to pages 241 and 242 in the text. © 2007 Lawrenceville Press

6 Chapter 10 Arrays with Meaningful Indexes
11/23/2018 5:18 AM Chapter 10 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 Refer to pages 242 and 243 in the text. © 2007 Lawrenceville Press

7 Chapter 10 The String Class
11/23/2018 5:18 AM Chapter 10 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() Refer to page 244 in the text. © 2007 Lawrenceville Press

8 Chapter 10 Unicode and char
11/23/2018 5:18 AM Chapter 10 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 , 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 Refer to pages 244 and 245 in the text. © 2007 Lawrenceville Press

9 Simplest searching algorithm
11/23/2018 5:18 AM Chapter 10 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. Refer to pages 246 and 247 in the text. © 2007 Lawrenceville Press

10 Chapter 10 Two-Dimensional Arrays
11/23/2018 5:18 AM Chapter 10 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: Refer to pages 247 and 248 in the text. © 2007 Lawrenceville Press

11 Chapter 10 Two-Dimensional Arrays
11/23/2018 5:18 AM Chapter 10 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 Refer to page 248 in the text. © 2007 Lawrenceville Press

12 Chapter 10 The ArrayList Class
11/23/2018 5:18 AM Chapter 10 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 Refer to pages 252 and 253 in the text. 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. © 2007 Lawrenceville Press

13 Chapter 10 Wrapper Classes
11/23/2018 5:18 AM Chapter 10 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() Refer to pages 253 and 254 in the text. © 2007 Lawrenceville Press

14 Chapter 10 Autoboxing and Auto-unboxing
11/23/2018 5:18 AM Chapter 10 Autoboxing and Auto-unboxing Eliminates the need to manually wrap and unwrap primitives for use in a collection Offers simpler, cleaner code because the wrapper class methods are no longer needed. Before autoboxing/unboxing: After autoboxing/unboxing: numbers.add(new Integer(5)); numbers.add(5); element = numbers.get(0); element = numbers.get(0); sum += element.intValue(); sum += element; Refer to pages 253 and 254 in the text. © 2007 Lawrenceville Press


Download ppt "Can store many of the same kind of data together"

Similar presentations


Ads by Google