Can arrays be stored in collections
1 posts in topic
Flat View  Flat View

Posted By:   Mehrdad_Shabestari
Posted On:   Friday, April 12, 2002 01:16 PM

How can I associate (store) array(s)in the value field of a Hashtable? Is such thing possible?

Basically I need to store and retrieve arrays of data associated with same key and be able to extract by array index.

Thanks in advance.

Re: Can arrays be stored in collections

Posted By:   Simon_Ablett  
Posted On:   Monday, April 15, 2002 06:11 AM

Yes, you can. See the example below which stores an array of 'Integer' objects in a hashtable.

import java.io.*;
import java.util.*;

public class TT
public static void main( String [] args )
// Create a hashtable and store an array of Integer
// objects in it...
Hashtable tab = new Hashtable();

Integer arr[] = { new Integer(5), new Integer(6), new Integer(7) };

tab.put(new String("AA"), arr);

// Now get the data back out again...
Enumeration iter = tab.elements();
Integer arr2[] = (Integer []) iter.nextElement();

for (int i=0; i< arr2.length; i++)
System.out.println("Element-"+i+": ("+arr2[i]+")");


About | Sitemap | Contact