We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Processing has trouble with Java "generics&am
Page Index Toggle Pages: 1
Processing has trouble with Java "generics&am (Read 578 times)
Processing has trouble with Java "generics&am
Sep 26th, 2007, 5:41am
 
I would like to create an "associative array" inside my Processing sketch,
as described at this web site:

http://en.wikipedia.org/wiki/Associative_array#Java

However, ... the Java syntax looks like this:

Code:

Map<String, String> phoneBook = new HashMap<String, String>();
phoneBook.put("Sally Smart", "555-9999");
phoneBook.put("John Doe", "555-1212");


... and Processing seems to choke on the "<" and ">" as if it doesn't recognize or accept that syntax.  I am not expert enough to know how to put these things off in a "pure Java" tab.

The use of "generics" in Java (and the corresponding syntax) seem to be relatively new, so maybe the Processing engine could be updated with the new grammar.

Alternatively, ... I wonder if some kind soul could explain how to implement an "associative array" in a "pure Java" tab, so that it can be called from regular Processing code.

thanks in advance,
 djones

Re: Processing has trouble with Java "generic
Reply #1 - Sep 26th, 2007, 10:50am
 
that's java 1.5 syntax, and you're right it's not supported by processing.

use hashtables, see here:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Hashtable.html

F
Re: Processing has trouble with Java "generic
Reply #2 - Sep 26th, 2007, 3:01pm
 
Thanks fjen.

I have a naive Java question.
In the Hashtable, they insert and retrieve a value this way:
Code:

Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
Integer n = (Integer)numbers.get("one");


Is it strictly necessary to convert an "int" to an "Integer" object for storage in a Hashtable?

thanks,
 djones
Re: Processing has trouble with Java "generic
Reply #3 - Sep 26th, 2007, 3:24pm
 
Hashtables can only store objects, and an int isn't an object, it's a primitive type, so you have to put it into an Interger, which is an object before you store it. And vice-versa you have to get the int out of an Integer when you retrieve the values.
Page Index Toggle Pages: 1