Ch. 7. Single-Dimensional Arrays

¡Supera tus tareas y exámenes ahora con Quizwiz!

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 in one step: - This shorthand syntax must be in ___ _________. - Using the shorthand notation, you have to _______, ________, and _______ the array all in ___ _________. - Splitting it would cause a _______ ________.

double[] myList = {1.9, 2.9, 3.4, 3.5}; - one statement - declare, create, and initialize in one statement - syntax error

Enhanced for Loop (for-each loop) Syntax:

for (elementType value: arrayRefVar) { // Process the value }

The Method for Finding a Key in the List: [Linear Search]

for (int i = 0; i < list.length; i++){ if (key == list[i]){ return i; } else return -1; }

Declaring and Creating in One Step:

datatype[] arrayRefVar = new datatype[arraySize]; (ex. double[] myList = new double[10];)

Declaring Array Variables:

datatype[] arrayRefVar; (ex. double[] myList;)

Array Default Values: When an array is created, its elements are assigned the default value of: - _______ for the numeric primitive data types - _______ for char types - _______ for boolean types

- 0 - '\u0000' - false

What is an Array

- A data structure, which can store a fixed-size collection of elements of the same data type. - A Collection of variables of the same type.

Array Storage: - All of the items placed into an array are automatically stored in ___________ memory locations. - As the program retrieves the value of each item (or "element") of an array, it simply moves in a __________ manner.

- All of the items placed into an array are automatically stored in adjacent memory locations. - As the program retrieves the value of each item (or "element") of an array, it simply moves from one memory location to the very next—in a sequential manner. It doesn't have to keep jumping around to widely scattered memory locations in order to retrieve each item's value.

Array Memory Allocation: - An array is stored so that the position of each element can be computed from its _______ tuple by a mathematical formula.

- An array is stored so that the position of each element can be computed from its index tuple by a mathematical formula.

What is an Anonymous Array

- An array where there is no explicit reference variable for the array. - Passed to an invoked method

- Arrays are useful mostly because the element indices can be computed at _____ ______. - This feature allows a _________ iterative statement to process arbitrarily many elements of an array.

- Arrays are useful mostly because the element indices can be computed at run time. - This feature allows a single iterative statement to process arbitrarily many elements of an array.

Java provides several overloaded sort methods for sorting an array, one of them is _________________ in the _____________ class.

- Arrays.sort, java.util.Arrays

The _________________ method can be used to return a string representation for the list.

- Arrays.toString(list)

- What is a Linear Search? - When Does the Search Stop? - What happens when a match is found? - What is the return when there is no match?

- Comparing the key element, key, sequentially with each element in the array list. - The method continues to do so until the key matches an element in the list or the list is exhausted without a match being found. - If a match is found, the search returns index - If no match is found, the search returns -1.

What Kind of Issues Array Data Structure Solve?

- Expedient: Instead of having to separately store related information in different variables (named memory locations), you can store them—as a collection—in just one variable. - Efficiency: It is more efficient for a program to access and process the information in an array, than it is to deal with many separate variables. - Memory Storage: Data stored in memory are accessed easier and faster.

Sorting Arrays: what is a selection sort?

- Finds the smallest number in the list and places it first. It then finds the smallest number remaining and places it second, and so on until the list contains only a single number.

Pass By Value: primitives - For a parameter of a primitive type value, the _____ ______ is passed. Changing the value of the local parameter inside the method _____ _____ affect the value of the variable outside the method.

- actual value, does not -

Processing Command-Line Parameters: - In the main method, get the arguments from args[0], args[1], ..., args[n], which corresponds to ____, ____, ..., ____ in the command line.

- arg0, arg1, ..., argn

The Length of an Array: - Once an array is created, its size is ________. It cannot be changed. You can find its size using ___________________.

- fixed - arrayRefVar.length

Binary Search: Result - The binarySearch method returns the ________ of the element in the list that matches the search key if it is contained in the list. Otherwise, it returns _____________ - The ______ ________ is the point at which the key would be inserted into the list.

- index , -insertion point - 1. - insertion point

Indexed Variables: - The array elements are accessed through the ________. - The array indices are ___________ > i.e, t starts from __ to __________________. - Each element in the array is represented using the following syntax, known as an ________ _________ : > _______________

- index. - 0-based > 0 to arrayRefVar.length-1. - indexed variable: > arrayRefVar[index];

The Arrays.binarySearch Method: - Java provides several overloaded binarySearch methods for searching a key in an array of int, double, char, short, long, and float in the ____________ class. - For the binarySearch method to work, the array must be pre-sorted in ____________ order.

- java.util.Arrays - increasing

Binary Search - For binary search to work, the elements in the array must already be ___________. Without loss of generality, assume that the array is in __________ ___________. - The binary search first compares the key with the element in the _________ of the array.

- ordered, ascending order - middle

Main Method Is Just a Regular Method: - You can call a regular method by passing actual _____. - Can you pass arguments to main?

- parameters - Yes

Java uses _____ _ ______ to pass arguments to a method.

- pass by value

Pass By Value: Arrays - For a parameter of an array type, the value of the parameter contains a ________ to an array; this __________ is passed to the method. Any changes to the array that occur inside the method body ______ _______ the original array that was passed as the argument.

- reference, will effect

Using Indexed Variables: - After an array is created, an indexed variable can be used in the same way as a _________ _________.

- regular variable

Enhanced for Loop (for-each loop): - A new for loop that enables you to __________ the complete array sequentially without using an _______ ________. - You still have to use an index variable if you wish to traverse the array in a ________ _________ or change the __________ in the array.

- traverse, index variable - different order, elements

Binary Search: 3 Cases - If the key is less than the middle element, ______ - If the key is equal to the middle element, ______ - If the key is greater than the middle element, ________

- you only need to search the key in the first half of the array. - the search ends with a match. - you only need to search the key in the second half of the array.

8 Processing Arrays Skills:

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)

Array Memory Allocation: (Example) - An array of 10 32-bit integer variables, with indices 0 through 9, may be stored as 10 words at memory addresses 2000, 2004, 2008, ... 2036, so that the element with index i has the address ______________

2000 + 4 × i.

All arrays consist of contiguous ___________ locations. The lowest address corresponds to the ______ element and the _______ address to the last element.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

An element is accessed by ___________ the array name. This is done by placing the _________ of the element within _____ ____ brackets after the name of the array.

An element is accessed by indexing the array name. This is done by placing the index of the element within square brackets after the name of the array.

Is this format correct? double[] myList; myList = {1.9, 2.9, 3.4, 3.5};

No. You must declare, create, and initialize in one statement.

Trace Program with Arrays

[...]

Creating Arrays:

arrayRefVar = new datatype[arraySize]; (ex. myList = new double[10];)

The arraycopy Utility Syntax:

arraycopy(sourceArray, src_pos, targetArray, tar_pos, length); Example: System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);

Printing arrays

for (int i = 0; i < myList.length; i++) { System.out.print(myList[i] + " "); }

Initializing arrays with random values

for (int i = 0; i < myList.length; i++) { myList[i] = Math.random() * 100; }

The JVM stores the array in an area of memory, called _______, which is used for _________ memory allocation where blocks of memory are allocated and freed in an arbitrary order.

heap, dynamic

Binary search Method:

int low = 0; int high = list.length -1; while (high >= low){ int mid = (low + high) /2; if (key < list[mid]) high = med - 1; else if (key == list[mid]) return mid; else low = mid + 1; } else return -1 - low;

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(); }


Conjuntos de estudio relacionados

AP Biology: Unit 2 Topic Questions Formative Assessment Part 1

View Set

Police Administration - Chapter 12

View Set

Prins. of Development Final Exam

View Set

Chapter 19: Nursing management of the pregnancy at risk:

View Set