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?

A

Yes

25
Q

What is an anonymous object?

A

An object that’s instantiated, but it’s not stored in a variable

26
Q

What two situations are anonymous objects typically used in?

A
  • 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
Q

What does “for-each loop” syntax look like in an ArrayList?

A

for (“element-type” “element-name” : “ArrayList-reference-variable”)

example: for (Bear bear : bears)

28
Q

What are the benefits of an ArrayList over a standard Array?

A
  • 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
Q

What are the benefits of a standard array over an ArrayList?

A
  • 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
Q

What is a LinkedList Class?

A

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
Q

What methods do ArrayList and LinkedList implement the same?

A

get, set, add, remove, clear, and size

32
Q

Why do ArrayList and LinkedList implement many of the same methods?

A

Because they both implement the same List interface

33
Q

What are implements?

A

They mean the class promises to implement all methods specified by the interface.

34
Q

What does the syntax look like for declaring a variable with an interface at the left?

A
35
Q

What are the beneifts of a LinkedList over and ArrayList?

A
  • 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
Q

What are the benefits of an ArrayList over a LinkedList?

A

Finding an indexed element is much faster than a LinkedList.

37
Q

What is a queue?

A

A first-in first-out (FIFO) waiting line.

38
Q

In a queue what order do you add and remove elements?

A

Add elements to the back (tail) and remove elements from the front(head).

39
Q

What class is the most efficient for implementing queues?

A

ArrayDeque

40
Q

What is ArrayDeque backed by?

A

An Array

41
Q

What is the length of the Array that ArrayDeque is backed by?

A

Initially 16, but its length doubles each time the current length is inadequate.

42
Q

What are the other traits of the backing array for ArrayDeque?

A
  • It is circular, meaning links connect opposite ends
  • Pointers identify current head and tail elements
  • Adding increments tail point, deleting increments head point.
43
Q

What is the syntax to access ArrayDeque?

A

import java.util.*;

44
Q

What is a stack?

A

A last-in first-out(LIFO) storage container.

45
Q

How does a stack add or remove elements?

A

It adds (push) and removes (pop) elements at the top.

46
Q

What are the two Java collections framework interface hierarchies?

A
  • Collection hierarchy
  • Map hierarchy
47
Q

What methods does the Collection interface specify?

A

One that are common to the List, Queue, and Set interfaces.

Such as, add, contains, isEmpty, set, size, and remove.

48
Q

What do classes that implement the Collection interface provide?

A

A one-parameter constructor that converts any type of collection to any other type of collection.

49
Q

What is the special queue included in the collection hierarchy?

A

A priority queue, which inserts higher priority elements closer to the head of the queue.

50
Q

What are the properties of a map?

A

Has a unique key, like an id number. This provides rapid access to other (more complex) objects.

51
Q

Java Collections Framework example

A
52
Q

Java Collections Framework - Maps example

A
53
Q

How does a map work?

A

It relates an object in a key set to another object that could be in another set, a list, or a queue.

54
Q

What is the input/output for a map?

A

The user provides an independent key and it returns a dependent value.

55
Q

How do you add, retrieve, and remove values in a map?

A
56
Q
A