Chapter 7 SingleDimensional Arrays Liang Introduction to Java



![Declaring Array Variables F datatype[] array. Ref. Var; Example: double[] my. List; Liang, Introduction Declaring Array Variables F datatype[] array. Ref. Var; Example: double[] my. List; Liang, Introduction](https://slidetodoc.com/presentation_image_h/1e6d3d6cab72311e270a6ed88da8bdb9/image-4.jpg)
![Creating Arrays array. Ref. Var = new datatype[array. Size]; Example: my. List = new Creating Arrays array. Ref. Var = new datatype[array. Size]; Example: my. List = new](https://slidetodoc.com/presentation_image_h/1e6d3d6cab72311e270a6ed88da8bdb9/image-5.jpg)
![Declaring and Creating in One Step F datatype[] array. Ref. Var = new datatype[array. Declaring and Creating in One Step F datatype[] array. Ref. Var = new datatype[array.](https://slidetodoc.com/presentation_image_h/1e6d3d6cab72311e270a6ed88da8bdb9/image-6.jpg)





![Array Initializers F Declaring, creating, initializing in one step: double[] my. List = {1. Array Initializers F Declaring, creating, initializing in one step: double[] my. List = {1.](https://slidetodoc.com/presentation_image_h/1e6d3d6cab72311e270a6ed88da8bdb9/image-12.jpg)
![Declaring, creating, initializing Using the Shorthand Notation double[] my. List = {1. 9, 2. Declaring, creating, initializing Using the Shorthand Notation double[] my. List = {1. 9, 2.](https://slidetodoc.com/presentation_image_h/1e6d3d6cab72311e270a6ed88da8bdb9/image-13.jpg)





![Finding the largest element double max = my. List[0]; for (int i = 1; Finding the largest element double max = my. List[0]; for (int i = 1;](https://slidetodoc.com/presentation_image_h/1e6d3d6cab72311e270a6ed88da8bdb9/image-19.jpg)
- Slides: 19
Chapter 7 Single-Dimensional Arrays Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1
Opening Problem Read one hundred numbers, compute their average, and find out how many numbers are above the average. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 2
Introducing Arrays Array is a data structure that represents a collection of the same types of data. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 3
Declaring Array Variables F datatype[] array. Ref. Var; Example: double[] my. List; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 4
Creating Arrays array. Ref. Var = new datatype[array. Size]; Example: my. List = new double[10]; references the first element in the array. my. List[9] references the last element in the array. my. List[0] Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 5
Declaring and Creating in One Step F datatype[] array. Ref. Var = new datatype[array. Size]; double[] my. List = new double[10]; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 6
The Length of an Array Once an array is created, its size is fixed. It cannot be changed. You can find its size using array. Ref. Var. length For example, my. List. length returns 10 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 7
Default Values When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, 'u 0000' for char types, and false for boolean types. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 8
Indexed Variables The array elements are accessed through the index. The array indices are 0 -based, i. e. , it starts from 0 to array. Ref. Var. length-1. In the example in Figure 6. 1, my. List holds ten double values and the indices are from 0 to 9. Each element in the array is represented using the following syntax, known as an indexed variable: array. Ref. Var[index]; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 9
Using Indexed Variables After an array is created, an indexed variable can be used in the same way as a regular variable. For example, the following code adds the value in my. List[0] and my. List[1] to my. List[2] = my. List[0] + my. List[1]; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 10
Analyze. Numbers. java F Page 257 Listing 7. 1 Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 11
Array Initializers F Declaring, creating, initializing in one step: double[] my. List = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand syntax must be in one statement. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 12
Declaring, creating, initializing Using the Shorthand Notation double[] my. List = {1. 9, 2. 9, 3. 4, 3. 5}; This shorthand notation is equivalent to the following statements: double[] my. List = new double[4]; my. List[0] = 1. 9; my. List[1] = 2. 9; my. List[2] = 3. 4; my. List[3] = 3. 5; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 13
CAUTION Using the shorthand notation, you have to declare, create, and initialize the array all in one statement. Splitting it would cause a syntax error. For example, the following is wrong: double[] my. List; my. List = {1. 9, 2. 9, 3. 4, 3. 5}; Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 14
Initializing arrays with input values java. util. Scanner input = new java. util. Scanner(System. in); System. out. print("Enter " + my. List. length + " values: "); for (int i = 0; i < my. List. length; i++) my. List[i] = input. next. Double(); Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 15
Initializing arrays with random values for (int i = 0; i < my. List. length; i++) { my. List[i] = Math. random() * 100; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 16
Printing arrays for (int i = 0; i < my. List. length; i++) { System. out. print(my. List[i] + " "); } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 17
Summing all elements double total = 0; for (int i = 0; i < my. List. length; i++) { total += my. List[i]; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 18
Finding the largest element double max = my. List[0]; for (int i = 1; i < my. List. length; i++) { if (my. List[i] > max) max = my. List[i]; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 19