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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T05:25:30+00:00 2026-06-17T05:25:30+00:00

In this code sample from page 114 of The Well-Grounded Java Developer , the

  • 0

In this code sample from page 114 of The Well-Grounded Java Developer, the last line:

Update[] updates = lu.toArray(new Update[0]);

contains the note: Pass zero-sized array, save allocation

List<Update> lu = new ArrayList<Update>();
String text = "";
final Update.Builder ub = new Update.Builder();
final Author a = new Author("Tallulah");

for (int i=0; i<256; i++) {
  text = text + "X";
  long now = System.currentTimeMillis();
  lu.add(ub.author(a).updateText(text).createTime(now).build());
  try {
    Thread.sleep(1);
  } catch (InterruptedException e) {
  }
}

Collections.shuffle(lu);
Update[] updates = lu.toArray(new Update[0]);

What allocation is this saving, exactly?

The javadoc for List#toArray(T[] a) mentions:

If the list fits in the specified array, it is returned therein.
Otherwise, a new array is allocated with the runtime type of the
specified array and the size of this list.

Which is what I remembered: if the array you pass to toArray(T[] a) can’t fit everything in the list, a new array is allocated. Plainly, there are 256 elements in the list, which cannot fit in an array of size 0, therefore a new array must be allocated inside the method, right?

So is that note incorrect? Or is there something else it means?

  • 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-17T05:25:31+00:00Added an answer on June 17, 2026 at 5:25 am

    Going off of @Andreas comment on the question, I think it is a typo, and should say:

    Pass zero-sized array, safe allocation.

    Because if you passed nothing to the method, you’ll end up calling the List#toArray() no-argument overload!

    This would return an Object[] (though it would contain nothing but Update instances) and would require changing the type of the updates variable, so the last line would become:

    Object[] updates = lu.toArray();
    

    And then every time you wanted to iterate over and use the elements in that array, you’d have to cast them to Update.

    Supplying the array calls the List#toArray(T[] a) method, which returns a <T> T[]. This array is reified to know it is an array of Update instances.

    So supplying an empty array of Updates results in an Update[] coming back from the toArray call, not an Object[]. This is a much more type-safe allocation! The word “save” in the note must be a typo!

    …this consumed way too much mental effort. Will post link to this in the book’s forums so they can correct it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a code sample taken from the Google Maps API developer's guide. I'm
I'm trying the code sample (for ASP NET C#, for VS2010) from this page:
i have this simple code to load data from other php page using get
I am new to scheme. This is code sample from SICP course of MIT.
I am learning to use xmlhttprequest/AJAX. In this sample code from w3schools, I do
I have downloaded the sample code from this site for FTP Client http://www.lysesoft.com/products/andftp/index.html But
When I have download sample code from this link and than try to build
I've been using the sample code from this d3 project to learn how to
I have this sample code for async operations (copied from the interwebs) public class
Another jquery calculation question. I've this, which is sample code from the plugin site

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.