Associative arrays in java?


are associative arrays supported in java? if they are, would you mind giving me a quick example(maybe just how to initialize them), thanks.

You can use Hashtables.

import java.util.Hashtable;

// in your class
Hashtable ht = new Hashtable();

// insert all elements into hashtable
ht.put( "first", "Sunday" );
ht.put( "second", "Monday" );

// get an element specified by it's key
String firstDayOfWeek = (String) ht.get( "first" );

You can also probably use ArrayLists.

Array list will accept anything and it converts it to objects however when you get the information back out again the object does not remeber what it went in as:

import java.util.ArrayList;

ArrayList myList = new ArrayList();

mylist.add( "String" );

// enters as a string

mylist.get( 0 );
// returns as an object even though it is a string

An ArrayList is not an associative array. Associative arrays consist of key value pairs like HashMap and Hashtable. ArrayList is simply a list of values.