 |
Author |
Topic: associative arrays (Read 506 times) |
|
Vair
|
associative arrays
« on: Nov 12th, 2004, 6:07pm » |
|
There's no mention of these in the reference section - is it possible to create them? I don't have any previous experience with Java syntax but from a quick squint at the docs it doesn't look like Java has them either. What I really want to do is to create a look up table for note frequencies so that I can easily look them up by name: freq = note['C4']; type of thing. What would be the best way to achieve this in processing? Could it be to make a custom class?
|
|
|
|
TomC
|
Re: associative arrays
« Reply #1 on: Nov 12th, 2004, 6:40pm » |
|
You're after a Hashtable. It doesn't work quite like you'd expect. Rather than array syntax, it uses put(key,value) and get(key). Additionally, it stores generic Objects which need to be converted (cast) back to the original type. Because ints and floats aren't Objects, in Java they have wrapper objects called Integer and Float. Code: Hashtable notes = new Hashtable(); notes.put("C4", new Integer(262)); // if you want more accuracy, use new Float(261.WhateverItIs) int freq = ((Integer)notes.get("C4")).intValue(); |
| As you can see, it's not exactly elegant. There are probably neater ways to do what you ask, like numbering all the notes and just using a regular array of ints or floats. This just got simpler in the most recent version of Java (but it's not well supported yet, and not supported in Processing), and I suspect it will be simpler in future versions of Processing.
|
« Last Edit: Nov 12th, 2004, 6:42pm by TomC » |
|
|
|
|
Vair
|
Re: associative arrays
« Reply #2 on: Nov 12th, 2004, 7:37pm » |
|
Thanks a lot - very helpful.
|
|
|
|
|