Adding in more work with Data Structures
This commit is contained in:
35
src/JavaMapAndDictionary.java
Normal file
35
src/JavaMapAndDictionary.java
Normal file
@@ -0,0 +1,35 @@
|
||||
import java.util.*;
|
||||
|
||||
public class JavaMapAndDictionary {
|
||||
public static void main(String[] args) {
|
||||
// Map Example
|
||||
double[] mapGPA = new double[] { 4.0, 3.0, 2.0, 1.0, 0.0 };
|
||||
char[] mapLetter = new char[] { 'A', 'B', 'C', 'D', 'F' };
|
||||
Map<Character, Double> map = new HashMap<Character, Double>();
|
||||
|
||||
for (int i = 0; i < mapGPA.length; i++) {
|
||||
map.put(mapLetter[i], mapGPA[i]); // Insert key/value into map
|
||||
}
|
||||
|
||||
for (Character key : map.keySet()) {
|
||||
System.out.println(key + " " + map.get(key));
|
||||
}
|
||||
|
||||
// END: Map Example
|
||||
System.out.println();
|
||||
// Dictionary Example
|
||||
double[] dictGPA = new double[] { 4.0, 3.0, 2.0, 1.0, 0.0 };
|
||||
char[] dictLetter = new char[] { 'A', 'B', 'C', 'D', 'F' };
|
||||
Dictionary<Character, Double> dictionary = new Hashtable<Character, Double>();
|
||||
|
||||
for (int i = 0; i < dictGPA.length; i++) {
|
||||
dictionary.put(dictLetter[i], dictGPA[i]); // Insert key/value into map
|
||||
}
|
||||
|
||||
for (Enumeration<Character> keys = dictionary.keys(); keys.hasMoreElements(); ) {
|
||||
char keyVal = keys.nextElement();
|
||||
System.out.println(keyVal + " " + dictionary.get(keyVal));
|
||||
}
|
||||
// END: Dictionary Example
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user