Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.

Similar presentations


Presentation on theme: "© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored."— Presentation transcript:

1 © 2007 Lawrenceville Press Slide 1 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:

2 © 2007 Lawrenceville Press Slide 2 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"};

3 © 2007 Lawrenceville Press Slide 3 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";

4 © 2007 Lawrenceville Press Slide 4 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); }

5 © 2007 Lawrenceville Press You do Pg 239 Review: StudentRoster Review: Squares Review: Reverse

6 © 2007 Lawrenceville Press Slide 6 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.

7 © 2007 Lawrenceville Press Slide 7 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

8 © 2007 Lawrenceville Press Slide 8 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()

9 © 2007 Lawrenceville Press Slide 9 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 00000000 01010110, which translates to 86 10.  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

10 © 2007 Lawrenceville Press Slide 10 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.

11 © 2007 Lawrenceville Press Slide 11 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:

12 © 2007 Lawrenceville Press Slide 12 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

13 © 2007 Lawrenceville Press Slide 13 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

14 © 2007 Lawrenceville Press Slide 14 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()

15 © 2007 Lawrenceville Press Slide 15 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;


Download ppt "© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored."

Similar presentations


Ads by Google