strucutres and their methods Flashcards

1
Q

How do you create an ArrayList object? Given an ArrayList “name”, add names to it

A

ArrayList<type> name = new ArrayList<type>();</type></type>

ArrayList<string> names = new ArrayList<string>();</string></string>

names. add(“Marty Stepp”);
names. add(“Stuart Reges”);

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

What are the methods for an ArrayList?

A

add(value)

appends value at end of list

add(index, value)

inserts given value just before the given index, shifting subsequent values to the right

clear()

removes all elements of the list

indexOf(value)

returns first index where given value is found in list (-1 if not found)

get(index)

returns the value at given index

remove(index)

removes/returns value at given index, shifting subsequent values to the left

set(index, value)

replaces value at given index with given value

size()

returns the number of elements in list

toString()

returns a string representation of the list

such as “[3, 42, -7, 15]”

addAll(list)

addAll(index, list)

adds all elements from the given list to this list

(at the end of the list, or inserts them at the given index)

contains(value)

returns true if given value is found somewhere in this list

containsAll(list)

returns true if this list contains every element from given list

equals(list)

returns true if given other list contains the same elements

iterator()

listIterator()

returns an object used to examine the contents of the list (seen later)

lastIndexOf(value)

returns last index value is found in list (-1 if not found)

remove(value)

finds and removes the given value from this list

removeAll(list)

removes any elements found in the given list from this list

retainAll(list)

removes any elements not found in given list from this list

subList(from, to)

returns the sub-portion of the list between
indexes from (inclusive) and to (exclusive)

toArray()

returns the elements in this list as an array

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

What are the benefits of an ArrayList over an array?

A

•seeing whether the value “Benson” is found

for (int i = 0; i < names.length; i++) {

if (names[i].equals(“Benson”)) { … }

}

if (list.contains(“Benson”)) { … }

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

What classes are used to implement a Set?

A
  • HashSet (using hash table array)
  • TreeSet (using a binary search tree)
  • LinkedHashSet - stores in order of insertion
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the cost to implement a HashSet and TreeSet?

A

HashSet = O(1)

TreeSet = O(log N)

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

How do you create a Set object?

A

Set<integer> set = new HashSet<integer>();</integer></integer>

Set<string> set2 = new TreeSet<string>();</string></string>

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

What are the Set class methods?

A

add(value)

adds the given value to the set

contains(value)

returns true if the given value is found in this set

remove(value)

removes the given value from the set

clear()

removes all elements of the set

size()

returns the number of elements in list

isEmpty()

returns true if the set’s size is 0

toString()

returns a string such as “[3, 42, -7, 15]”

addAll(collection)

adds all elements from the given collection to this set

containsAll(coll)

returns true if this set contains every element from given set

equals(set)

returns true if given other set contains the same elements

iterator()

returns an object used to examine set’s contents (seen later)

removeAll(coll)

removes all elements in the given collection from this set

retainAll(coll)

removes elements not found in given collection from this set

toArray()

returns an array of the elements in this set

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

What is the for each loop?

A

•Provides a clean syntax for looping over the elements of a Set, List, array, or other collection

Set<double> grades = new HashSet<double>();</double></double>

for (double grade : grades) {

System.out.println(“Student’s grade: “ + grade);

}

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

What is the keySet method? What can we do with the key set?

A
  • returns a Set of all keys in a map
  • We can loop over the keys in a foreach loop and we can get key’s associated value by calling get on the map
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do we create a Map object?

A

Map<key> name = new HashMap<key></key></key>

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

What is a map object?

A

holds a set of key-value pairs where each key is unique

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

Whagt are the basic map operations?

A

put(key, value): adds a mapping from a akley to a value

get(key): retrieves the value mapped to the key

remove(key): removes the given key and its mapped value

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

What are the map methods?

A

put(key, value)

adds a mapping from the given key to the given value;
if the key already exists, replaces its value with the given one

get(key)

returns the value mapped to the given key (null if not found)

containsKey(key)

returns true if the map contains a mapping for the given key

remove(key)

removes any existing mapping for the given key

clear()

removes all key/value pairs from the map

size()

returns the number of key/value pairs in the map

isEmpty()

returns true if the map’s size is 0

toString()

returns a string such as “{a=90, d=60, c=70}”

keySet()

returns a set of all keys in the map

values()

returns a collection of all values in the map

putAll(map)

adds all key/value pairs from the given map to this map

equals(map)

returns true if given map has the same mappings as this one

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

Suppose we have a list of students and their score on a test. We want to create a map that allows us to find which students got what scores, how would we do that

A

Need to map each score to a collection of students:

Map<double>&gt; studentScore = new HashMap<double>&gt;();</double></double>

studentScore.put (10.5, new TreeSet<string>());</string>

studentScore.get(3.6).add(“Jared”);

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

What’s the issue with accessing elements of Sets and Maps?

A

They can’t be accessed by index

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

What’s the issue with using a for each loop with Maps and Sets?

A

For each loops are read-only so we can’t modify while looping

17
Q

What is an iterator?

A

an object that allows a client to traverse the elements of a collection

18
Q

What are the Iterator methods?

A

hasNext()

returns true if there are more elements to examine

next()

returns the next element from the collection (throws a NoSuchElementException if there are none left to examine)

remove()

removes the last value returned by next() (throws an IllegalStateException if you haven’t called next() yet)

19
Q

How do we access the iterator() method of a collection?

A

Set<string> set = new HasSet<string>();</string></string>

Iterator<string> itr = set.iterator();</string>

20
Q
A
21
Q
A
22
Q
A
23
Q
A