• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Understanding an ArrayList - p137 (Java OCA 8 Programmer I Study Guide, Sybex)

 
Greenhorn
Posts: 1
Objective C C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

On p137: code-line 8
I do not understand what this code does:
String[] stringArray = list.toArray(new String[0]);
specifically the new String[0] part.
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

James Tedesco wrote:specifically the new String[0] part

  • It 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.
  • <T> T[] toArray(T[] a) method allows you specify runtime type of the returned array but Object[] toArray() retruns an array of Object.
  • Read more about these methods, click here --->toArray(T[] a) and here --->Object[] toArray()


  • In your example specified array is of type String so It retruns array of String type. If you try to compile code of example 1, It gives compile time error saying Type mismatch: cannot convert from String[] to Integer[] because It is returning an array of String type and we are trying to assign It to array of Integer type.
  • Example1:

  • If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)

  • Example 2: Please read commentsOutput:
    Contents of listOne
    No of elements in List listOne is: 2
    One
    Two
    ***************************************
    String array tempOne with size 0
    New Array returned and assigned to stringArrayOne

    Contents of stringArrayOne
    Size of stringArrayOne is: 2
    One
    Two
    ***************************************
    String array tempTwo with size 5
    Same Array tempTwo returned and assigned to stringArrayTwo

    Contents of stringArrayTwo
    Size of stringArrayTwo is: 5
    One
    Two
    null
    null
    null
     
    Sheriff
    Posts: 11606
    178
    Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi James Tedesco,

    First of all, a warm welcome to CodeRanch!

    James Tedesco wrote:I do not understand what this code does:
    String[] stringArray = list.toArray(new String[0]);
    specifically the new String[0] part.


    The toArray() method will (according to the javadoc of the Collection interface) return an array containing all elements in this collection. And this method comes in two versions: one version without parameters and another one with an array parameter.
    The first version will return an Object array containing all elements of this list. Here's a code exampleSo the return type of this method is always Object[]. But what if you want to convert the list of Strings into a String array? Enter the second version of this method (with the array parameter). It lets you define the runtime type of the array to contain the collection elements. If you create an array which is not big enough to store all collection elements, a new array will be created at runtime to store the collection elements. Consider this code snippetSo from the output of line4, you clearly see that a different array is created. And new String[0] is the code to create an empty array, so is the short-hand syntax forNow let's see what happens if the array parameter is big enough to hold all collection elementsAnd if you run this code, you get the following output:
    true
    [one, two, three, null, six, five, four, three, two, one]
    [one, two, three, null, six, five, four, three, two, one]


    So two important things to note: if the array is big enough to store all collection elements, no other array is created and thus the array used as a parameter is the same as the array returned by the toArray() method. If the array has more elements than the collection, the element in the array immediately following the end of the collection is set to null.

    Hope it helps!
    Kind regards,
    Roel

    PS. Please always QuoteYourSources and please note that many OCAJP8 study guides are currently available, so only mentioning a page number is not enough.
    reply
      Bookmark Topic Watch Topic
    • New Topic