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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:32:38+00:00 2026-06-14T20:32:38+00:00

list.toArray(T[] a) , what if the T is a runtime type? List interface in

  • 0

list.toArray(T[] a), what if the T is a “runtime type”?

“List” interface in Java, the method: T[] toArray(T[] a)
OK, it’s a quite old question, usually we use it like that:

String[] array = list.toArray(new String[0]);
String[] array = list.toArray(new String[list.size()]);

My problem is, I don’t know what the type “T” is when I was coding, the type “T” can only be decide in runtime,here is example:

List list=...;  // no generic type declared on this List
Class<?> clazz=...; //yeah, this is the real type of the element of the list, I can sure about that

Now I need to convert the list into an array which element type is clazz, what should I do?

May be you are wondering why do I have this kind of need:
I am making a little modification onto Morphia(It’s a java-mongoDB framework). I need to aquire data from mongoDB and set it into right field of a POJO. In certain circumstance, the field type is array, the data aquired is a BasicDBList entity(which extends ArrayList),so the BasicDBList entity had to be convert into an array which type is compatible with the POJO filed.

I looked into source code of ArrayList, then achieved my goals with some ugly code:

List list=...;
Class<?> clazz=...;
Object targetArray=Array.newInstance(clazz, list.size());
list.toArray((Object[])targetArray);

Is there any better way to do this please?

  • 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-06-14T20:32:40+00:00Added an answer on June 14, 2026 at 8:32 pm

    With @arne.b ‘s help in the comments, I think there is no better way to do this:

    List list=...;
    Class<?> clazz=...;
    Object[] targetArray=(Object[])Array.newInstance(clazz, list.size());
    Object[] myArray=list.toArray(targetArray);
    

    Then I can set it to field without error:

    Field field=...;    //ComponentType of this field can be sure is "clazz"
    field.set(entity,myArray);
    

    Or I can do this to convert it to it’s actrual type manually:

    Class<?> arrayClazz=Class.forName("[L"+clazz.getName()+";");
    arrayClazz.cast(myArray);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is how I create the array var list = $(#sortable).sortable('toArray'); $.ajax({ type: 'POST',
i have a List of records type Item = { Color : string; Size
How can I convert a List<Integer> to int[] in Java? I'm confused because List.toArray()
I have a collection of type List that I want to convert to SomeType[].
Does anybody know why Java 1.6 has this behaviour: List<String> list = ArrayList<String>(); String[]
I have this ArrayList in Java - List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
The ajax feature is on route http://mysite/playlist-edit/3 Javascript: $(#applySort).click(function(){ var list = $(#sortable).sortable('toArray'); $.post({
I'm using .Net 3.5 (C#) and I've heard the performance of C# List<T>.ToArray is
What happens when we use the lock object? I am aware that it the
My scenerio is like this: At runtime, I bind ToolStripComboBox to array of struct:

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.