Final Review: Memory Management and Data Structures

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/74

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

75 Terms

1
New cards

ArrayList

An ArrayList stores a list of elements in contiguous memory locations.

2
New cards

get(i)

Gets the element at index i - fast.

3
New cards

set(i, element)

Replaces the element at index i with the specified element - fast.

4
New cards

add(element)

Appends the specified element to the end of the list - fast.

5
New cards

add(i, element)

Inserts the specified element at index i - slow.

6
New cards

remove(i)

Removes the element at index i in the list - slow.

7
New cards

Linked List

A linked list does not use contiguous memory locations.

<p>A linked list does not use contiguous memory locations.</p>
8
New cards

Linked List Item

Each item in a linked list contains data and a pointer to the next item's location in memory.

9
New cards

ArrayList vs Linked List

ArrayList stores items in contiguous memory; Linked List stores each item anywhere in memory.

10
New cards

Static Variable

A class level variable; only a single copy is created and shared among all objects of the class.

11
New cards

Stack

The region where a method's local variables are allocated during a method call.

<p>The region where a method's local variables are allocated during a method call.</p>
12
New cards

Heap

The region where the 'new' operator allocates memory for objects.

<p>The region where the 'new' operator allocates memory for objects.</p>
13
New cards

LinkedList Class

The LinkedList class implements the List interface.

14
New cards

List Interface

Defines a collection of ordered elements; elements can be accessed by their integer index.

15
New cards

LinkedList Declaration

LinkedList aList = new LinkedList();

16
New cards

Common LinkedList Method: get(index)

Returns element at specified index.

17
New cards

Common LinkedList Method: set(index, newElement)

Replaces element at specified index with newElement; returns element previously at specified index.

18
New cards

Common LinkedList Method: add(newElement)

Adds newElement to the end of the List; List's size is increased by one.

19
New cards

add()

Adds newElement to the List at the specified index. Indices of the elements previously at that specified index and higher are increased by one. List's size is increased by one.

20
New cards

size()

Returns the number of elements in the List.

21
New cards

remove(index)

Removes element at specified index. Indices for elements from higher positions are decreased by one. List size is decreased by one. Returns reference to element removed from List.

22
New cards

remove(existingElement)

Removes the first occurrence of an element which is equal to existingElement. Indices for elements from higher positions are decreased by one. List size is decreased by one. Returns true if specified element was found and removed.

23
New cards

Enhanced For Loop

A loop that iterates through a List using the syntax: for (element_type variable : array or collection) { //body of loop }.

24
New cards

Map

The Map interface defines a collection that maps keys to values (e.g., mapping student IDs to student names).

25
New cards

HashMap

The HashMap class implements the Map interface.

26
New cards

put()

Associates key with specified value. If key already exists, replaces previous value with specified value.

27
New cards

putIfAbsent()

Associates key with specified value if the key does not already exist or is mapped to null.

28
New cards

get()

Returns the value associated with key. If key does not exist, return null.

29
New cards

containsKey()

Returns true if key exists, otherwise returns false.

30
New cards

containsValue()

Returns true if at least one key is associated with the specified value, otherwise returns false.

31
New cards

ex. List.size()

Returns 0 when List is empty.

32
New cards

ex. List.add(8)

List is now: 8

33
New cards

HashMap

K represents the key type, V represents the value type. K and V must be reference types (e.g., String, Integer, Person).

34
New cards

remove(key)

If the key exists, it deletes that key-value pair and returns the value that was associated with that key. If the key doesn't exist, it returns null.

35
New cards

ex. Map.clear()

Map is now empty

36
New cards

keySet()

Returns a Set containing all keys within the map.

37
New cards

values()

Returns a Collection containing all values within the map.

38
New cards

Queue interface

Defines a Collection of ordered elements that supports element insertion only at the tail and element removal only from the head.

39
New cards

LinkedList class

Implements the Queue interface.

40
New cards

Queue myQueue = new LinkedList()

T represents the element's type and T must be a reference type.

41
New cards

add(newElement)

Adds newElement element to the tail of the queue. The queue's size increases by one.

42
New cards

remove()

Removes and returns the element at the head of the queue. Throws an exception if the queue is empty.

43
New cards

poll()

Removes and returns the element at the head of the queue if the queue is not empty. Otherwise, returns null.

44
New cards

element()

Returns, but does not remove, the element at the head of the queue. Throws an exception if the queue is empty.

45
New cards

peek()

Returns, but does not remove, the element at the head of the queue if the queue is not empty. Otherwise, returns null.

46
New cards

Set

The Set interface defines a Collection of unordered unique elements.

47
New cards

Example of Set

{red, green, blue} is a set and {red, green, blue} = {blue, red, green}.

48
New cards

Non-Set Example

{red, green, green, blue} is NOT a set as it contains duplicate elements.

49
New cards

HashSet

The HashSet class implements the Set interface.

50
New cards

Importing HashSet

To use HashSet, import java.util.HashSet.

51
New cards

Declaring HashSet

A HashSet can be declared and created as follows: HashSet hashSet = new HashSet(); T is a reference type that represents the HashSet's element type.

52
New cards

add() Method

If element does not exist, adds element to the set and returns true. If element already exists, returns false.

53
New cards

remove() Method

If element exists, removes element from the set and returns true. If the element does not exist, returns false.

54
New cards

contains() Method

Returns true if element exists, otherwise returns false.

55
New cards

size() Method

Returns the number of elements in the set.

56
New cards

Recursive Method

A recursive method is a method that calls itself.

57
New cards

Base Case

The base case returns without performing a recursive call and stops the recursion from continuing on forever.

58
New cards

Recursive Case

The recursive case calls the method itself.

59
New cards

Binary Search Algorithm

The binary search algorithm is a very efficient algorithm for guessing your partner's number.

60
New cards

Binary Search

An algorithm that begins at the midpoint of a range and halves the range after each guess.

61
New cards

midVal

Calculated as midVal = (lowVal + highVal) / 2.

62
New cards

Lower Guess

If your partner says 'lower', then guess in the range [lowVal, midVal-1].

63
New cards

Higher Guess

If your partner says 'higher', then guess in the range [midVal+1, highVal].

64
New cards

Recursive Algorithm

An algorithm that is applied again on a smaller range after each guess.

65
New cards

Recursive Binary Search

Used to quickly find an item in a sorted list stored in an array or ArrayList.

66
New cards

findMatch Method

A method that performs recursive binary search of an item in a list within the range lowVal to highVal.

67
New cards

Base Case 1

If item matches the middle element in the range [lowVal, highVal], then return the index of the middle element.

68
New cards

Base Case 2

If the range size is 1 (i.e., lowVal = highVal), then return -1 to indicate that the item is not found.

69
New cards

Recursive Call - Lower Half

Call findMatch(list, item, lowVal, midVal-1) if item < middle element.

70
New cards

Recursive Call - Higher Half

Call findMatch(list, item, midVal+1, highVal) if item > middle element.

71
New cards

Stack Region

A part of a program's memory reserved to support method calls.

<p>A part of a program's memory reserved to support method calls.</p>
72
New cards

Stack Frame

A structure that stores a method's parameters and local variables.

73
New cards

Stack Overflow

Occurs when a stack frame extends beyond the memory region allocated for the stack, often causing a program crash.

74
New cards

Stack Overflow Error

A report generated when a program crashes due to stack overflow.

75
New cards

Non-Recursive Algorithm

An alternative approach developed to avoid stack overflow in large problems.