CC_108_Chapter_8.ppt - 2 Read one hundred numbers compute their average and find out how many numbers are above the average 3 To describe why arrays are

# CC_108_Chapter_8.ppt - 2 Read one hundred numbers compute...

This preview shows page 1 - 17 out of 68 pages.

2
Read one hundred numbers, compute their average, and find out how many numbers are above the average. 3
To describe why arrays are necessary in programming To declare array reference variables and create arrays To initialize the values in an array To access array elements using indexed variables To declare, create, and initialize an array using an array initializer To program common array operations (displaying arrays, summing all elements, finding min and max elements, random shuffling, shifting elements) To copy contents from one array to another To develop and invoke methods with array arguments and return value To define a method with variable-length argument list To use the methods in the Arrays class 4
5 Array is a data structure that represents a collection of the same types of data. 5.6 4.5 3.3 13.2 4 34.33 34 45.45 99.993 11123 double[] myList = new double[10]; myList reference myList[0] myList[1] myList[2] myList[3] myList[4] myList[5] myList[6] myList[7] myList[8] myList[9] Element value Array reference variable Array element at index 5
datatype[] arrayRefVar; Example: double[] myList; datatype arrayRefVar[]; // This style is allowed, but not preferred Example: double myList[]; 6
arrayRefVar = new datatype[arraySize]; Example: myList = new double[10]; myList[0] references the first element in the array. myList[9] references the last element in the array. 7
datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10]; datatype arrayRefVar[] = new datatype[arraySize]; double myList[] = new double[10]; 8
Once an array is created, its size is fixed. It cannot be changed. You can find its size using arrayRefVar.length For example, myList.length returns 10 9
When an array is created, its elements are assigned the default value of 0 for the numeric primitive data types, '\u0000' for char types, and false for boolean types. 10
The array elements are accessed through the index. The array indices are 0-based , i.e., it starts from 0 to arrayRefVar.length-1. In the example, myList 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 : arrayRefVar[index]; 11
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 myList[0] and myList[1] to myList[2] . myList[2] = myList[0] + myList[1]; 12
Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement. 13
double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand notation is equivalent to the following statements: double[] myList = new double[4]; myList[0] = 1.9; myList[1] = 2.9; myList[2] = 3.4; myList[3] = 3.5; 14
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[] myList; myList = {1.9, 2.9, 3.4, 3.5}; 15
public class Test { public static void main(String[] args) { int[] values = new int[5]; for (int i = 1; i < 5; i++) { values[i] = i + values[i-1]; } values[0] = values[1] + values[4]; } } 16 Declare array variable values, create an array, and assign its reference to values

#### You've reached the end of your free preview.

Want to read all 68 pages?

• Spring '19
• Array, myList