Chapter 7 – Arrays and Array Lists Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Chapter Goals (cid:72) To become familiar with using arrays and array lists (cid:72) To learn about wrapper classes, auto-boxing and the generalized for loop (cid:72) To study common array algorithms (cid:72) To learn how to use two-dimensional arrays (cid:72) To understand when to choose array lists and arrays in your programs (cid:72) To implement partially filled arrays T To understand the concept of regression testing Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Arrays (cid:72) Array: Sequence of values of the same type (cid:72) Construct array: new double[10] (cid:72) Store in variable of type : double[] double[] data = new double[10]; (cid:72) When array is created, all values are initialized depending on array type: (cid:72) Numbers: 0 (cid:72) Boolean: false (cid:72) Object References: null Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Arrays Use to access an element: [] values[2] = 29.95; Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Arrays (cid:72) Using the value stored: System.out.println("The value of this data item is " + values[2]); (cid:72) Get array length as (Not a method!) values.length (cid:72) Index values range from to 0 length - 1 (cid:72) Accessing a nonexistent element results in a bounds error: double[] values = new double[10]; values[10] = 29.95; // ERROR (cid:72) Limitation: Arrays have fixed length Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Declaring Arrays Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Arrays Syntax 7.1 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Self Check 7.1 What elements does the data array contain after the following statements? double[] values = new double[10]; for (int i = 0; i < values.length; i++) values[i] = i * i; Answer: 0, 1, 4, 9, 16, 25, 36, 49, 64, 81, but not 100 Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24 Self Check 7.2 What do the following program segments print? Or, if there is an error, describe the error and specify whether it is detected at compile-time or at run-time. a) double[] a = new double[10]; System.out.println(a[0]); b) double[] b = new double[10]; System.out.println(b[10]); c) double[] c; System.out.println(c[0]); Answer: a) 0 b) a run-time error: array index out of bounds c) a compile-time error: c is not initialized Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Jeff Staruch Wednesday, May 27, 2015 at 9:30:17 AM Eastern Daylight Time 34:15:9e:2f:35:24
Description: