Chapter 10 - Arrayslist/Java Collections Flashcards

1
Q

What does the ArrayList class provide?

A

The basic functionality that comes with a standard array, plut it provides additional functionality

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

What is the basic functionality of an ArrayList?

A

Stores an ordered collection of values and allows access to the value via an index.

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

What is the added functionality of an ArrayList?

A

Grows and shrinks dynamically by inserting and deleting elements at any specified location.

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

What do you type to import the ArrayList class?

A

import java.util.ArrayList;

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

How do you initialize an ArrayList reference variable?

A

Use this syntax in a declaration:

ArrayList(Student) variablename = new ArrayList<>();

It should be angled brackets around Student instead of parathesis <>

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

Comparative Syntax for creating ArrayList objects, regular objects and standard Arrays

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

What syntax adds and element to the end of an ArrayList?

A

ArrayList-reference-variable.add(item);

The “item” that’s being added must be the same type as the type specificied in the ArrayList’s declaration.

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

What does API stand for?

A

Application Programming Interface

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

What is the Java API?

A

The interface to the huge libaray of pre-built Java classes.

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

What do you have to use to interface with with Java API classes?

A

Their public methods

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

What information do you have to know to use a public method?

A

The type of argument to pass to it, and what type of value it returns.

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

What does a method’s API show?

A

The method’s parameters and its return type.

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

ArrayList objects don’t use square brackets like standard arrays when accessing and updating an element, what do they use instead?

A

A get method to access the element

A set method to update and element

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

What does the E return type in the ArrayList’s get method mean?

A

It stands for element. Represents the data types of the ArrayList’s elements.

It should be the same as the element-type specified in the ArrayList’s initialization:

ArrayList(element-type) reference-variable = new ArrayList<>();

It should be angled brackets around element-type <>

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

What does the set method allow you to do?

A

Assign a value to an existing ArrayList.

API heading example:

public E set(int index, E elem)

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

What does the following Syntax do?

public void add(int index, E elem)

A

Starting with the specified index position, shift the original elements to higher-indexed positions. Then insert the elem parameter at the specified index position.

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

What does the following Syntax do?

public void clear()

A

Removes all elements from the list.

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

What does the following Syntax do?

public int indexOf(Object elem)

(Object is a generic class that can be used as a class type for any object)

A

Search for the first occurrence of the elem parameter within the list. If it’s found, return its index position. If it’s not found, return -1.

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

What does the following syntax do?

public boolean isEmpty()

A

Return true is the list contains no elements.

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

What does the following syntax do?

public E remove(int index)

A

Remove the element at the specified index position, shift all higher-indexed element to lower-indexed positions, and return the removed element.

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

What does the following syntax do?

public int size()

A

Return the number of elements in the list.

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

What is the output if you print of concatenate an ArrayList?

A

A comma-seperated list of ArrayList elements surrounded by square brackets.

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

How do you store primitives in an ArrayList?

A

You have to wrap the primitives in wrapper classes.

In Java 5.0 and higher it’s done automatically if a wrapper class is used in an ArrayList declaration.

24
Q

When storing objects in an ArrayList can you create and object and add it to the ArrayList in the same statement?

25
What is an anonymous object?
An object that's instantiated, but it's not stored in a variable
26
What two situations are anonymous objects typically used in?
- Passing a new created object into a method or constructor. bears. add(new Bear("Acme", "brown teddy")); - Returning a newly created object from a method. return new Bear(maker, type);
27
What does "for-each loop" syntax look like in an *ArrayList?*
for ("element-type" "element-name" : "ArrayList-reference-variable") example: for (Bear bear : bears)
28
What are the benefits of an *ArrayList* over a standard Array?
- It's easy to increase the size of an ArrayList - just call *add* - It's easy for a programmer to insert or remove an element to or from the interior of an ArrayList - just call add or remove and specify the element's index position
29
What are the benefits of a standard array over an *ArrayList?*
- A standard array uses []'s to access array elements (which is easier than using get and set methods) - A standard array is more efficient with storing primitive values
30
What is a *LinkedList* Class?
Similar to an ArrayList in that is hold a collection of related data, but instead of using an underlying array to store the data, it uses a chain of references.
31
What methods do *ArrayList* and *LinkedList* implement the same?
get, set, add, remove, clear, and size
32
Why do *ArrayList* and *LinkedList* implement many of the same methods?
Because they both implement the same List interface
33
What are *implements?*
They mean the class promises to implement all methods specified by the interface.
34
What does the syntax look like for declaring a variable with an interface at the left?
35
What are the beneifts of a *LinkedList* over and *ArrayList?*
- It's easier to find, add, or remove element that are near one of the two ends. - Once an element has been found, it's easier to remove it from a *LinkedList* (you just change a pair of references) rather than an *ArrayList* (shift all higher elements to lower array indices)
36
What are the benefits of an *ArrayList* over a *LinkedList*?
Finding an indexed element is much faster than a *LinkedList.*
37
What is a *queue*?
A first-in first-out (FIFO) waiting line.
38
In a queue what order do you add and remove elements?
Add elements to the back (tail) and remove elements from the front(head).
39
What class is the most efficient for implementing queues?
*ArrayDeque*
40
What is *ArrayDeque* backed by?
An Array
41
What is the length of the Array that *ArrayDeque* is backed by?
Initially 16, but its length doubles each time the current length is inadequate.
42
What are the other traits of the backing array for *ArrayDeque?*
- It is circular, meaning links connect opposite ends - Pointers identify current head and tail elements - Adding increments tail point, deleting increments head point.
43
What is the syntax to access *ArrayDeque*?
import java.util.\*;
44
What is a stack?
A last-in first-out(LIFO) storage container.
45
How does a stack add or remove elements?
It adds (push) and removes (pop) elements at the top.
46
What are the two Java collections framework interface hierarchies?
- Collection hierarchy - Map hierarchy
47
What methods does the *Collection* interface specify?
One that are common to the List, Queue, and Set interfaces. Such as, add, contains, isEmpty, set, size, and remove.
48
What do classes that implement the *Collection* interface provide?
A one-parameter constructor that converts any type of collection to any other type of collection.
49
What is the special queue included in the collection hierarchy?
A priority queue, which inserts higher priority elements closer to the head of the queue.
50
What are the properties of a map?
Has a unique key, like an id number. This provides rapid access to other (more complex) objects.
51
Java Collections Framework example
52
Java Collections Framework - Maps example
53
How does a map work?
It relates an object in a key set to another object that could be in another set, a list, or a queue.
54
What is the input/output for a map?
The user provides an independent *key* and it returns a dependent *value.*
55
How do you add, retrieve, and remove values in a map?
56