If you step through this in a debugger, you'll find that the cast exception
is actually coming from this line:

   return (Hashtable[]) stuff.toArray();

Remember that in Java, an object (the general object, not the specific class
Object) "knows" what it really is. And also remember that in Java an array
can be thought of and treated as an object. The Vector's toArray() method is
defined to return an object whose type is array-of-Objects. So trying to
cast that to the type array-of-Hashtables will get you the cast exception.

Now, since the objects that have been put into the Vector are, in fact,
Hashtables (each individual object "knows" that it's a Hashtable), you can
certainly cast the individual elements to Hashtable. But you have to go
through the array-of-Objects first. Something like this:

private Object[] getStuff() {
   Vector stuff = new Vector();
   Hashtable ht = new Hashtable();
   ht.put("A", "Stuff A");
   ht.put("B", "Stuff B");
   stuff.add(0, ht);
   return stuff.toArray();
 }

Object[] hashArray = getStuff();
for (int idx=0; idx<hashArray.length; idx++) {
        Hashtable oneHash = (Hashtable)hashArray[idx];
        // Do something with oneHash . . .
}

--Jim Preston

----- Original Message -----
From: "Scott Costa" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, May 17, 2000 10:15 AM
Subject: Offtopic: Quick Java programming question...


> I'm trying to write a method which returns an array of Hashtables, and in
> the method, I am assembling the data as a vector, and trying to user
> "toArray()" to return the data as an array.
> Something like this:
>
> private Hashtable[] getStuff() {
>   Vector stuff = new Vector();
>   Hashtable ht = new Hashtable();
>   ht.put("A", "Stuff A");
>   ht.put("B", "Stuff B");
>   stuff.add(0, ht);
>   return (Hashtable[]) stuff.toArray();
> }
>
> This compiles great, but when I execute the line:
> Hashtable[] test = getStuff();
>
> I get a java.lang.ClassCastException.  The only way I have been able to
get
> this to work, is to have the function return "Object[]", and explicitly
cast
> the returned object everytime I reference it, but I would like to make the
> function simply return an array of Hashtable objects.
>
> Any ideas on why the code above doesn't work, or how I can get it to work?
>
> Thanks,
> Scott Costa
> [EMAIL PROTECTED]
>
>
===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
>  http://java.sun.com/products/jsp/faq.html
>  http://www.esperanto.org.nz/jsp/jspfaq.html
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
>  http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to