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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:50:35+00:00 2026-05-25T11:50:35+00:00

I have a method that uses the varargs feature: void add(Animal …); Now, instead

  • 0

I have a method that uses the varargs feature:

void add(Animal ...);

Now, instead of doing .add(dog, cat), I have an Animal list with unknown number of elements,

List<Animal> i = new ArrayList<Animal>();
i.add(dog);
i.add(cat);

and want to call add with the elements of this list.

I think I could use an array, but when I do .add(i.toArray()), it gives a compiler error.

What is the proper way to do it?

  • 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-25T11:50:35+00:00Added an answer on May 25, 2026 at 11:50 am

    It’s:

    add(i.toArray(new Animal[i.size()]))
    

    List.toArray returns an Object[], regardless of the type argument on the List: even though you might write new List<String>().toArray(), you will get an Object[]. However, the version of toArray that takes an array to fill returns an array with the correct type: if you write new List<String>().toArray(new String[0]), you will get an String[]. Note that the size of the array you pass in doesn’t even have to match the size of the list, although it’s good practice to ensure that it does.

    This is ultimately due to a mildly tricky feature of generics. At first glance, you might think that String[] and List<String>mean similar things for their base types – one is an array of strings, the other is a list of strings.

    However, they are in fact very different.

    An array is a language primitive, and has its type baked into it. If you took a hex editor and looked at an array instance in memory in the JVM, you would be able to find (somewhere nearby) a record of the type of objects it holds. That means that if you take an instance of an array of some unknown component type, you can find out what that type is. Conversely, it means that if you’re going to create an instance of an array, you need to know what component type you want.

    The List, on the other hand, uses generics, which in Java is implemented with type erasure, which means that, roughly speaking, it is something that exists in the compiler, but not at runtime (the compiler can check that you get it right, but the JVM can’t). This leads to a simple and efficient implementation (one simple enough to have been added to pre-generics Java without changing the JVM), but it has some shortcomings – in particular, that at runtime, there is no way to tell what the type argument on any particular instance of a generic class is, because type arguments only exist in the compiler. Because it is up to the List instance to handle toArray(), the only thing it can do is create an Object[]. It just doesn’t know of a more specific type to use.

    One way of looking at this is that arrays have a type argument as part of their class, whereas Lists have a type argument as part of their type, and since objects have classes but variables have types, you can’t get the type argument of a List from an object, only from a variable holding an object (as an aside, you also can’t get the type argument of an array from a variable holding an array (consider Object[] array = new String[0];), but that doesn’t really matter because, the variable lets you get hold of an object – unless it’s null).

    To boil this down to code, the problem is:

    public <E> E[] createSimilarlyTypedArray(List<E> list) {
        Class<E> componentType = list.???; // there is no way to do this
        return Arrays.newInstance(componentType, list.size());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that uses the params keyword, like so: private void ParamsMethod(params
I am starting out with unit testing, I have a method that uses the
I have a method set up that uses jquery form for a file upload
I have a method I'm writing that uses reflection to list a class's static
I have an application (ASP.NET MVC) that uses a Next New method to get
I have a model that uses a acts_as_nested_set fork, and I've added a method
I have a method that uses setAttribute on the Request object. I need to
I have a normalization method that uses the normal distribution functions pnorm() and qnorm().
I have a vc++ method that uses fprintf to write values to a file
I have a data fetching method that uses jQuery.ajax() to fetch xml files. /*

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.