is 4415 midterm 2
Finding the largest element
double max = myList[0]; for (int i = 1; i < myList.length; i++) { if (myList[i] > max) max = myList[i]; }
Summing all elements
double total = 0; for (int i = 0; i < myList.length; i++) { total += myList[i]; }
Declaring, creating, initializing Using the Shorthand Notation
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;
Trace Program with Arrays
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]; } } where after: values[i] = i + values[i-1]; value[1] is 1
Trace Program with Arrays
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]; } }
Trace Program with Arrays
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]; } } where after i++, i becomes 2
Trace Program with Arrays
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]; } } where when for int i = 1 i becomes 1
Trace Program with Arrays
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]; } } where int[] values = Declare array variable values, create an array, and assign its reference to values
Array Initializers
✦ Declaring, creating, initializing in one step: double[] myList = {1.9, 2.9, 3.4, 3.5}; This shorthand syntax must be in one statement.
Declaring and Creating in One Step
✦ datatype[] arrayRefVar = new datatype[arraySize]; double[] myList = new double[10]; ✦ datatype arrayRefVar[] = new datatype[arraySize]; double myList[] = new double[10];
Declaring Array Variables
datatype[] arrayRefVar; Example: double[] myList; ✦ datatype arrayRefVar[]; // This style is allowed, but not preferred Example: double myList[];
Shifting Elements
-
Processing Arrays (types of processing)
1. (Initializing arrays with input values) 2. (Initializing arrays with random values) 3. (Printing arrays) 4. (Summing all elements) 5. (Finding the largest element) 6. (Finding the smallest index of the largest element) 7. (Random shuffling) 8. (Shifting elements)
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 myList[0] and myList[1] to myList[2]. myList[2] = myList[0] + myList[1];
Array
Array is a data structure that represents a collection of the same types of data.
The Length of an Array
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
Indexed Variables
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 in Figure 6.1, 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];
CAUTION with shorthand notation
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};
Creating Arrays
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.
Random shuffling
for (int i = 0; i < myList.length - 1; i++) { // Generate an index j randomly int j = (int)(Math.random() * myList.length); // Swap myList[i] with myList[j] double temp = myList[i]; myList[i] = myList[j]; myList[j] = temp; }
Initializing arrays with random values
for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; }
Printing arrays
for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); }
Initializing arrays with input values
java.util.Scanner input = new java.util.Scanner(System.in); System.out.print("Enter " + myList.length + " values: "); for (int i = 0; i < myList.length; i++) myList[i] = input.nextDouble();