Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confuse actual size of backed array from arralist list

 
Edgar Trania
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what happen to this code construction of array sized 0

this code produced output 4
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the documentation for that method say should happen?
 
Edgar Trania
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah it will return all list elements to array
but im trying to say that i got confused with array semantics but nevermind then
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So are you clear about what is happening there? Or do you still have a question?
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edgar Trania wrote:but im trying to say that i got confused with array semantics but nevermind then

Yes, it is a bit tricky.

The problem is that it's basically impossible to return a properly-typed generic array (in your case, a String[]), without either:
  • (a) The class of each element.
  • (b) A "sample" (non-null) element, or
  • (c) A sample target array (also non-null).

  • I'm guessing the designers chose the latter as the best compromise, since there's nothing to stop you writing:
    String[] sa = sl.toArray(new String[s1.size()]);

    and it will probably run quicker too.

    HIH

    Winston
     
    Mansukhdeep Thind
    Ranch Hand
    Posts: 1158
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Paul Clapham wrote:So are you clear about what is happening there? Or do you still have a question?


    I have a question Paul. :) I read the documentation of this method. It says that
    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.


    So, in this case, since the String array being passed is of a size less than the number of elements in the list, a new String[] array will be created at run-time having a size 4. Correct?
     
    Winston Gutkowski
    Bartender
    Pie
    Posts: 10417
    63
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Mansukhdeep Thind wrote:So, in this case, since the String array being passed is of a size less than the number of elements in the list, a new String[] array will be created at run-time having a size 4. Correct?

    Correct. Hence my suggestion above.

    Winston
     
    Mansukhdeep Thind
    Ranch Hand
    Posts: 1158
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Winston.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic