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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:09:04+00:00 2026-06-17T22:09:04+00:00

I want to convert an ArrayList of a generic type to an array of

  • 0

I want to convert an ArrayList of a generic type to an array of generic types(the same generic type). For exaple I have ArrayList<MyGeneric<TheType>> and I want to obtain MyGeneric<TheType>[].

I tried using the toArray method and casting:

(MyGeneric<TheType>[]) theArrayList.toArray()

But this does not work. My other option is to create an array of MyGeneric<TheType> and insert one by one the elements of the arraylist, casting them to the correct type.
But Everything I tried to create this array failed.

I know I have to use Array.newInstance(theClass, theSize) but how do I obtain the class of MyGeneric<TheType>? Using this:

Class<MyGeneric<TheType>> test = (new MyGeneric<TheType>()).getClass();

Does not work. The IDE states that Class<MyGeneric<TheType>> and Class<? extends MyGeneric> are incompatible types.

Doing this:

    Class<? extends MyGeneric> test = (new MyGeneric<TheType>()).getClass();

    MyGeneric[] data = (MyGeneric[]) Array.newInstance(test, theSize);

    for (int i=0; i < theSize; i++) {
        data[i] = theArrayList.get(i);
    }
    return data;

Raises a ClassCastException at the line data[i] = ....

What should I do?

Note:

I need the array because I have to use it with a third-party library so “use a insert-the-name-of-the-collection-here” is not an option.

  • 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-17T22:09:05+00:00Added an answer on June 17, 2026 at 10:09 pm

    Create an array of your type first, then you should call:

    <T> T[]   toArray(T[] a)
    

    Returns an array containing all of the elements in this list in proper
    sequence (from first to last element); the runtime type of the
    returned array is that of the specified array.

    Read the specification:
    http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#toArray(T%5B%5D)

    If your array is shorter than the list, a new array will be allocated and returned by the method; if your array is longer than the list, the rest items in the array will be set to null.

    ———– example ———-

    ArrayList<MyGeneric<TheType>> list;
    //......
    MyGeneric<TheType>[] returnedArray = new MyGeneric[list.size()]; // You can't create a generic array. A warning for an error. Don't mind, you can suppress it or just ignore it.
    returnedArray = list.toArray(returnedArray); // Cong! You got the MyGeneric<TheType>[] array with all the elements in the list.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert an ArrayList to a List<string> using LINQ. I tried ToList()
I want to know if it is safe/advisable to convert from ArrayList to Array?
I am using Flex SDK 4.5 and I want to Convert ArrayCollection to ArrayList
I have an ArrayList<String> named listout, I want to convert it to an ArrayList<SelectItem>
I want to convert String array to ArrayList . For example String array is
i have this arraylist and i want to put the data in array.but i
I have an ArrayList<JCheckBox> that i want to convert to an ArrayList<String> First I
I have vehicle data object returning string array .I want to convert or cast
So imagine I have the following: myList of type ArrayList<CustomType> and I want to
I have a resource string array that I want to put into an ArrayList,

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.