strucutres and their methods Flashcards
How do you create an ArrayList object? Given an ArrayList “name”, add names to it
ArrayList<type> name = new ArrayList<type>();</type></type>
ArrayList<string> names = new ArrayList<string>();</string></string>
names. add(“Marty Stepp”);
names. add(“Stuart Reges”);
What are the methods for an ArrayList?
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
What are the benefits of an ArrayList over an array?
•seeing whether the value “Benson” is found
for (int i = 0; i < names.length; i++) {
if (names[i].equals(“Benson”)) { … }
}
if (list.contains(“Benson”)) { … }
What classes are used to implement a Set?
- HashSet (using hash table array)
- TreeSet (using a binary search tree)
- LinkedHashSet - stores in order of insertion
What is the cost to implement a HashSet and TreeSet?
HashSet = O(1)
TreeSet = O(log N)
How do you create a Set object?
Set<integer> set = new HashSet<integer>();</integer></integer>
Set<string> set2 = new TreeSet<string>();</string></string>
What are the Set class methods?
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
What is the for each loop?
•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);
}
What is the keySet method? What can we do with the key set?
- 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 do we create a Map object?
Map<key> name = new HashMap<key></key></key>
What is a map object?
holds a set of key-value pairs where each key is unique
Whagt are the basic map operations?
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
What are the map methods?
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
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
Need to map each score to a collection of students:
Map<double>> studentScore = new HashMap<double>>();</double></double>
studentScore.put (10.5, new TreeSet<string>());</string>
studentScore.get(3.6).add(“Jared”);
What’s the issue with accessing elements of Sets and Maps?
They can’t be accessed by index