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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:27:20+00:00 2026-05-31T20:27:20+00:00

I am wondering why did they design the toArray method in ArrayList to take

  • 0

I am wondering why did they design the toArray method in ArrayList to take a input of an array in Java?

    ArrayList<String> listArray = new ArrayList<String>();

    listArray.add("Germany");
    listArray.add("Holland");
    listArray.add("Sweden");

    String []strArray = new String[3];
    String[] a = (String [])listArray.toArray(strArray);

To me it appears that, they dont need this input because the instance of the ArrayList itself has enough details to convert the data into an array.

My question is why do they still need the array to be passed in? Thanks.

  • 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-31T20:27:21+00:00Added an answer on May 31, 2026 at 8:27 pm

    Two reasons I can think of:

    1. Erasure means that the generic parameters aren’t available at runtime, so an ArrayList<String> doesn’t know that it contains strings, it’s just the raw type ArrayList. Thus all invocations of toArray() would have to return an Object[], which isn’t strictly correct. You’d have to actually create a second array of String[] then iterate over the first, casting all of its parameters in turn to come out with the desired result type.
    2. The way the method is defined means that you can pass in a reference to an existing array, and have this populated via the method. In some cases this is likely very convenient, rather than having a new array returned and then copying its values elsewhere.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking at google calendar and wondering how they did their reminders. Currently
I did some programming with Java and C# and they both have great GUI
I'm wondering if updating statistics has helped you before and how did you know
Wondering how to open many new windows with Javascript. I have found plenty of
Wondering if anyone knows a nice way to execute a Java command-line program from
I really like the search function on iA and I was wondering how they
Specifically, I'm curious about how they did the phone number entry part, where they
I was wondering if they was a way to prevent some commands from being
I have been tasked with a project where I am to add a new
I am just wondering why did asp.net team choose / as the default value

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.