CSE 331 Lecture Notes - Lecture 12: Foreach Loop, Iterator, Junit

31 views3 pages
16 Jun 2018
School
Course
Professor
Maps
Holds a set of unique keys and collection of values, and a key is associated with a value
Also called a “dictionary”
Operations
o put(key, value): adds key and value to the map
o get(key, value): retrieves value mapped to key
o remove(key): removes key and its associated value
Implementing: can be implemented by an array or linkedlist.
Traversing Data
An array will be using a for-loop
A list will be using a for-each loop
o for (T item: list) {
System.out.println(item);
Iterators: java interface that dictates how a collection of data should be traversed
o hasNext() returns true if iteration has more elements
o next() returns the nenxt element in iteration
While (iterator.hasNext()) {
T item = iterator.next();
}
// doesn’t handle same key with different values
Public class KaseyDictionary<K, V> implements Iterable<K> { //uses generics
Private kaseyNode front;
Private int size;
Public KaseyDictionary() {
This.front = null;
This.size = 0;
}
Private class kaseyNode {
Public k key;
Public v value;
Public kaseyNode next;
Public kaseyNode(K key, V value, kaseyNode next) {
This.key = key;
This.value = value;
This.next = next;
}
}
Public void put(K key, V value) {
kaseyNode temp = New kaseyNode(key, value, front);
this.front = temp;
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Document Summary

Implementing: can be implemented by an array or linkedlist. Traversing data: an array will be using a for-loop, a list will be using a for-each loop for (t item: list) { Iterators: java interface that dictates how a collection of data should be traversed: hasnext() returns true if iteration has more elements, next() returns the nenxt element in iteration. // doesn"t handle same key with different values. Public class kaseydictionary implements iterable { //uses generics. Public kaseynode(k key, v value, kaseynode next) { Public void put(k key, v value) { kaseynode temp = new kaseynode(key, value, front); this. front = temp; this. size++; Testing: software test: a separate piece of code that exercises the code you are assessing by providing input to your code and finishes with an assertion of what the result should be, 1. Isolate: break your code into small modules: 2. Build in increments: make a plan from simplest to most complex cases: 3.

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents