Chapter 7 Arrays Flashcards
Declaring Array Variables: (syntax)
datatype[] arrayRefVar;
ex: double[] myList;
Creating Arrays: (syntax)
datatype[] arrayRefVar = new datatype[arraySize];
ex:
double[] myList = new double[10];
myList[0] = the FIRST element in the array
myList[9] = is the last element in the array[10]
The length of an array: (syntax)
Find size using:
arrayRefVar.length
Find last index using:
arrayRefVar.length-1
*An out of bounds error will occur if you attempt to reference elements beyond the bounds of an array
Default values:
When an array is created, its elements are assigned the default value of:
0 for numeric primitive tyoes
‘\u0000’ for char types
false for boolean types
Indexed Variables/using them
The. number in the brackets. Can be used like regular variables.
arrayRefVar[index];
myList[2] = myList[0] + myList[1];
Declaring, creating, and initializing using the shorthand notation
double[] myList = {1.9, 2.9, 3.4, 3.5};
8 Ways of processing arrays
- Initializing arrays with input values
- Printing arrays
- Initializing arrays with random values
- Summing all elements
- Finding the largest element
- Random Shuffling
- Shifting elements
Initializing arrays with input values
Scanner input = new Scanner(System.in);
System.out.print(“Enter “ + myList.length + “ values: “);
for (int i = 0; i < myList.length; i++){
myList[i] = input.nextDouble();
}
Printing arrays
for (int i = 0; i < myList.length; i++){
System.out.print(myList[i] + “ “);
}
or
for-each
for(int i: myList){
System.out.print(i);
}
Initializing arrays with random values:
for (int i = 0; i < myList.length; i++){
myList[i] = Math.random() * 100;
}
Summing all elements
double total = 0;
for (int i = 0; i < myList.length; i++){
total += myList[i];
}
Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++){
if (myList[i] > max) {
max = myList[i];
}
}
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;
}
Shifting elements
double temp = myList[0] // Retain the first element
// Shift elements left
for (int [i] = 1; i < myList.length; i++){
myList[i - 1] = myList[i];
}
// Move the first element to fill the last position
myList[myList.length - 1] = temp;
Enhanced for loop (for each loop)
Display the following code displays all elements in the array myList
*Note you still have to use an index variable if you wish to traverse the array in a different order or change the elements in the array
// The following code displays all elements in the array myList
for (double value: myList){
System.out.println(value);
}
// The syntax is:
for (elementType value: arrayRefVar){
// Process the value
}
Copying arrays:
Duplicating an array or part of an array
list2 = list1;
Copying arrays:
Using a loop
int[] source array = {2,3,1,5,10};
int[] targetArray = new int [sourceArray.length];
for (int i = 0; i < sourceArrays.length; i++)
targetArray[i] = sourceArray[i];
Passing arrays to methods
public static void printArray(int[] array){
for (int i = 0; i < array.length; i++){
System.out.print(array[i] + “ “ );
}
}
// Invoke the method
int[] list = {3,1,2,6,4,2};
printArray(list);
//Invoke the method (anonymous array)
printArray(new int[] {3,1,2,6,4,2});
The array copy Utility Syntax
arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);
Anonymous array syntax
new dataType[] {literal 1, 2, 3, };
Pass by value: Primitive types.
For a parameter of this type the actual value is passed. Changing the value of the local parameter does not affect the value of the variable outside the method.
Pass by value: Array types.
For a parameter of this type the value contains a reference to an array; this reference us passed to the method. Any changes to the array that occur inside the method body will effect the original array that was passed as the argument.
Heap memory
The JVM stores the array in an area of memory called heap, which is used for dynamic memory allocation where blocks of memory are allocated and freed in arbitrary order.
Call stack
Keeps track of method calls. First in last out.