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

  • Home
  • SEARCH
  • 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 have a some JPA entities that inherit from one another and uses discriminator
How does one convert an image from one color profile to another (screen to
Before I get pointed to one of those 'VS.' questions like below... ASP.NET webforms
Ok, this is another one in the theory realm for the CS guys around.
There are two databases in SQL Server 2005: One called A and another one
The current top-voted to this question states: Another one that's not so much a
I'm writing a programmer's text editor (yes another one) in Perl called Kephra ,
I'm developing a small utility application that needs to detect whether another one has
Another easy one hopefully. Let's say I have a collection like this: List<DateTime> allDates;
All the articles I've found via google are either obsolete or contradict one another.

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.