Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 94239
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:28:13+00:00 2026-05-10T23:28:13+00:00

Just for review, can someone quickly explain what prevents this from working (on compile):

  • 0

Just for review, can someone quickly explain what prevents this from working (on compile):

private HashSet data;  ...  public DataObject[] getDataObjects( ) {     return (DataObject[]) data.toArray(); } 

…and what makes this the way that DOES work:

public DataObject[] getDataObjects( ) {     return (DataObject[]) data.toArray( new DataObject[ Data.size() ] ); } 

I’m not clear on the mechanism at work with casting (or whatever it is) that makes this so.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T23:28:13+00:00Added an answer on May 10, 2026 at 11:28 pm

    Because toArray() creates an array of Object, and you can’t make Object[] into DataObject[] just by casting it. toArray(DataObject[]) creates an array of DataObject.

    And yes, it is a shortcoming of the Collections class and the way Generics were shoehorned into Java. You’d expect that Collection<E>.toArray() could return an array of E, but it doesn’t.

    Interesting thing about the toArray(DataObject[]) call: you don’t have to make the ‘a’ array big enough, so you can call it with toArray(new DataObject[0]) if you like.

    Calling it like toArray(new DateObject[0]) is actually better if you use .length later to get the array length. if the initial length was large and the same array object you passed was returned then you may face NullPointerExceptions later

    I asked a question earlier about Java generics, and was pointed to this FAQ that was very helpful: http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok, this has got to be a super simple problem. I just can't seem
Wordpress 3.0 just came out and it was cited that one can make a
I just installed Restful Authentication. After someone has successfully logged in, they write a
Just what the title says, I need to change the password for an existing
Just looking for the first step basic solution here that keeps the honest people
just a quick question: I am a CS undergrad and have only had experience
Just bought a 2.4GHz Intel Core 2 Duo iMac with 2GB of memory and
Just getting my head around Ruby metaprogramming. The mixin/modules always manage to confuse me.
Just how much slower are events? I have written a streaming XML parser (that
just wondering if anyone knows of a truly restful Put/delete implementation asp.net mvc preview

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.