Chapter 7 Arrays Flashcards

1
Q

Declaring Array Variables: (syntax)

A

datatype[] arrayRefVar;

ex: double[] myList;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Creating Arrays: (syntax)

A

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]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The length of an array: (syntax)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Default values:
When an array is created, its elements are assigned the default value of:

A

0 for numeric primitive tyoes
‘\u0000’ for char types
false for boolean types

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Indexed Variables/using them

A

The. number in the brackets. Can be used like regular variables.

arrayRefVar[index];
myList[2] = myList[0] + myList[1];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Declaring, creating, and initializing using the shorthand notation

A

double[] myList = {1.9, 2.9, 3.4, 3.5};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

8 Ways of processing arrays

A
  1. Initializing arrays with input values
  2. Printing arrays
  3. Initializing arrays with random values
  4. Summing all elements
  5. Finding the largest element
  6. Random Shuffling
  7. Shifting elements
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Initializing arrays with input values

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Printing arrays

A

for (int i = 0; i < myList.length; i++){
System.out.print(myList[i] + “ “);
}
or
for-each
for(int i: myList){
System.out.print(i);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Initializing arrays with random values:

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Summing all elements

A

double total = 0;
for (int i = 0; i < myList.length; i++){
total += myList[i];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Finding the largest element

A

double max = myList[0];
for (int i = 1; i < myList.length; i++){
if (myList[i] > max) {
max = myList[i];
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Random shuffling

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Shifting elements

A

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;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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

A

// 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
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Copying arrays:

Duplicating an array or part of an array

A

list2 = list1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Copying arrays:

Using a loop

A

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];

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Passing arrays to methods

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

The array copy Utility Syntax

A

arraycopy(sourceArray, src_pos, targetArray, tar_pos, length);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Anonymous array syntax

A

new dataType[] {literal 1, 2, 3, };

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Pass by value: Primitive types.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Pass by value: Array types.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Heap memory

A

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.

24
Q

Call stack

A

Keeps track of method calls. First in last out.

25
Q

swap method

A

ex swap(int n1, int n2);

26
Q

Returning an array from a method

List {1,2,3,4,5,6} becomes
List 2 {6,5,4,3,2,1}

A

int[] list1 = {1,2,3,4,5}
int[] list2 = reverse(list1);

public static int[] reverse (int[] list){
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1; i < list.length; i++, j++);{
result[j] = list[i];
}
return result;
}

or

int temp;
for( int i = 0; i < arr.length / 2; i++){
temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length -1 - i] = temp;
}
}

27
Q

Variable-length arguments

A variable number of arguments of the same type can be passed to a method and treated as an array.

A

typeName… parameterName

public class searching{
public static void main (String[] args){
printValues(1,2,3,4,5);
}
public static void printValues(int…arr){
for (int x: arr){
System.out.print(x + “ “);
}
System.out.println();
}
}

28
Q

Searching Arrays:
List the two types

A

linear search and binary search

29
Q

Searching arrays:

Describe a method for finding a key in the list, such as whether a certain score is included in the list of scores.

A

public class linear search {
public static int linearSearch(int[] list, int key){
for (int i = 0; i < list.length; i++)
if (key == list[i])
return i;
return -1;
}
}

30
Q

Linear Search (Basic search)

The linear search approach compares the key element, key, sequentially with each element in the array list.

This method continues to do so until the key matches an element in the list or the list is exhausted without a match being found.

A

If a match is made, the linear search returns the index of the element in the array that matches the key, if no match is found, the search returns -1.

/** The method for finding a key in the list */
public static int linearSearch(int[] list, int key) {
for (int i = 0; i < list.length; i++)
if (key == list[i])
return i;
return -1;
}

31
Q

Binary search

*The elements must already be in ascending order

Narrowing search

A

The binary search compares the key with the element in the middle of the array

The binary method returns the INDEX of the element in the list that matches the search key if it is contained in the list

32
Q

Binary Search: if the key is less than the middle element

A

you only have to search the first half of the array

33
Q

Binary search: if the key is equal to the middle element

A

the search ends with a match

34
Q

Binary search: if the key is greater than the middle element

A

you only need to search the key in the second element

35
Q

Use binary search to find the key in the list

A

public static int binarySearch(int[] list, int key){
int low = 0;
int high - list.length - 1;

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

36
Q

Since binary search is frequently used in programming, Java provides several overloaded binarySearch methods for searching a key in an array of int, double, char, short, long, and float in the

A

java.util.Arrays class.

int[] list = {2, 4, 7, 10, 11, 45, 50, 59, 60, 66, 69, 70, 79};
System.out.println(“Index is “ +
java.util.Arrays.binarySearch(list, 11));

//return is 4

index = java.util.Arrays.binarySearch(agesOfZombies, age);
if (index >= 0){
System.out.println(“The zombie with the age of “ + age +
“ is at index “ + index);
}else{
System.out.print(“No match”);
}

37
Q

Selection sort definition

A

Selection sort finds the smallest number in the list and places it in first. Then it finds the smallest number remaining and places it in second, and so on until the list contains only a single number.

38
Q

Selection sort method

A

public static void selectionSort(double[] list){
for(int i = 0; i < list.length; i++){

//Find the minimum in the length
  double currentmin = list[i];
  int currentMinIndex = i;
  for (int j = i + 1; j < list.length; j++){
    if (currentMin > list[j]){
      currentMin = list[j];
      currentMinIndex = j;
  }
}
// swap list[i] with list[currentMinIndex} if necessary;
   if currentMinIndex != i) {
      list[currentMinIndex] = list[i];
       list[i] = currentMin;
}    } }
39
Q

What is overloading a method

A

naming two methods with the same name but differentiating by using different parameters

40
Q

Overloading sort methods for arrays of numbers

A

double[] numbers = {1.1,2.2,3.3,4.4,5.5};

java.util.Arrays.sort(numbers);

41
Q

Overloading sort methods for arrays of chars

A

char[] chars = {‘a’, ‘A’, ‘4’, ‘F’, ‘D’, ‘P’};
java.util.Arrays.sort(chars);

42
Q

Java 8 multicore for fast sorting

A

Arrays.parallelSort(list)

43
Q

The method to return a string representation for the list

A

Arrays.toString(list)

44
Q

Processing command line parameters

(Passing in Casper Dracula Frankenstein in terminal as arguments)

A

In the main method, get the arguments from args[0], args[1], …, args[n], which corresponds to arg0, arg1, …, in the command line

45
Q

A variable is declared using the syntax:

A

elementType[] arrayRefVar;

46
Q

Unlike declarations for primitive data type variables, the declaration of an array variables does what:

A

the declaration of an array does not allocate any space in memory for the array

47
Q

Is an array variable a primitive data type?

A

No. An array variable contains a reference to an array.

48
Q

You can only assign elements to an array if:

A

It has already been created.

49
Q

Create a new array:

A

dataType[] arrayRefVar = new elementType[arraySize];

50
Q

An index (arrayRefVar[index]) must be an:

A

integer or an integer expression

*this is the syntax for representing each element in the array

51
Q

After an array is created, its size becomes permanent and can be obtained using:

A

arrayRefVar.length-1

*an out of bounds error will occur if you attempt to reference elements beyond the bounds of an array

52
Q

Index off by one error

A

mistaking the first element with index 1 instead of 0

53
Q

When an array is created, its elements are assigned the default values of:

A

0 for numeric and primitive data types
‘\u000’ for char types
false for boolean

54
Q

Java uses short hand notation which is known as the array initializer which combines declaring an array, creating an array, and initializing an array in one statement, using the syntax:

A

elementType[] arrayRefVar = {value0, value1, …, valuek};

55
Q

When you pass an array argument to a method, you are actually passing the _______ of the array.

A

reference.

The called method can modify the elements in the callers original array.

56
Q

If an array is sorted, _____ search is more efficient than ______ search for finding an element in the array.

A

binary search is more efficient than linear search for finding an element in a sorted array.

57
Q

What does the selection sort method do:

A

Selection sort finds the smallest number in the list and swaps it with the first element.

It then finds the smallest number remaining and swaps it with the first element in the remaining list and so on, until only a single number remains.