Use a Hashtable

by Sanju 2010-03-04 09:55:36

Use a Hashtable

Hashtables are very useful for maintaining a 1-to-1 relationship between Objects. With an arbitary key, you can retrieve easily an Object. In the following example, we retrieve a String array with a String.

String[] array1 = {"A","B","C"};
String[] array2 = {"X","Y","Z"};
String[] array3 = {"1","2","3"};

Hashtable arrays = new Hashtable();
arrays.put("Array1", array1);
arrays.put("Array2", array2);
arrays.put("Array3", array3);

String[] resultArray = (String[])(arrays.get("Array2"));
System.out.println
(resultArray[0] + ", " + resultArray[1] + ", " + resultArray[2]);

Tagged in:

1641
like
0
dislike
0
mail
flag

You must LOGIN to add comments