add new String/Int/FloatDict constructors for easier initialization

This commit is contained in:
Ben Fry
2015-04-30 13:51:09 -04:00
parent 21af93ac28
commit 125cf5b943
5 changed files with 73 additions and 0 deletions

View File

@@ -69,6 +69,7 @@ public class StringDict {
}
}
/**
* @nowebref
*/
@@ -84,6 +85,29 @@ public class StringDict {
}
}
/**
* Constructor to allow (more intuitive) inline initialization, e.g.:
* <pre>
* new StringDict(new String[][] {
* { "key1", "value1" },
* { "key2", "value2" }
* });
* </pre>
* It's no Python, but beats a static { } block with HashMap.put() statements.
*/
public StringDict(String[][] pairs) {
count = pairs.length;
this.keys = new String[count];
this.values = new String[count];
for (int i = 0; i < count; i++) {
keys[i] = pairs[i][0];
values[i] = pairs[i][1];
indices.put(keys[i], i);
}
}
/**
* @webref stringdict:method
* @brief Returns the number of key/value pairs