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 3594194
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:40:49+00:00 2026-05-18T19:40:49+00:00

Another one of those how do I do toArray() with no warnings questions, but

  • 0

Another one of those how do I do toArray() with no warnings questions, but it’s different from most of the ones posted here.

How do I rewrite the method implementation (without changing the method signature) to make it compile without warning?

The apparent difficulty here is that the Class information for T is not available during runtime. However, the return type is an erased type during runtime too, so there’s no really reason that this cannot be done. So, how do I do this if want to enforce compile-time type safety?

Thank you

<T> GenericClass<T>[] toGenericArray(List<GenericClass<T>> list) {
    return list.toArray(new GenericClass[0]);
}
  • 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. Editorial Team
    Editorial Team
    2026-05-18T19:40:50+00:00Added an answer on May 18, 2026 at 7:40 pm

    You can’t. Because Arrays are covariant, it is impossible to have compile time safety for an array that’s holding a parameterized type. (unless you use < ? > which is legal.)

    This will always be legal:

    GenericClass<String>[] array = new GenericClass[1];
    Object[] brokenArray = array;
    brokenArray[0] = new GenericClass<Integer>();
    String value = array[0].getGenericValue(); //kaboom
    

    Compiler is unable to protect you from that, so it forces you to acknowledge/suppress that there’s a warning.

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

Sidebar

Related Questions

I posted another one on this,, but i had to edit it really much...
Yet another one of these, however none of the answers supplied on those posted
I've read several different questions from SO about this already but still haven't been
So I'm afraid this is another one of those slideshow questions that clutters up
When sending data from one iOS device to another, I receive these errors. 2012-06-21
Here's another one for you to help me solve: I have an ASP.NET website
This post is closely related to another one I posted some days ago .
I hope this is not one of those questions where I slap myself afterwards,
Possible Duplicate: Get first N key pairs from an Ordered Dictionary to another one
This isn't another one of those How can I record audio in the browser?

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.