File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Confuse actual size of backed array from arralist list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confuse actual size of backed array from arralist list" Watch "Confuse actual size of backed array from arralist list" New topic
Author

Confuse actual size of backed array from arralist list

Edgar Trania
Greenhorn

Joined: Feb 14, 2013
Posts: 7
what happen to this code construction of array sized 0

this code produced output 4
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

What does the documentation for that method say should happen?
Edgar Trania
Greenhorn

Joined: Feb 14, 2013
Posts: 7
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So are you clear about what is happening there? Or do you still have a question?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7552
    
  18

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

    Isn't it funny how there's always time and money enough to do it WRONG?
    Articles by Winston can be found here
    Mansukhdeep Thind
    Ranch Hand

    Joined: Jul 27, 2010
    Posts: 1157

    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?


    ~ Mansukh
    Winston Gutkowski
    Bartender

    Joined: Mar 17, 2011
    Posts: 7552
        
      18

    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

    Joined: Jul 27, 2010
    Posts: 1157

    Thank you Winston.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Confuse actual size of backed array from arralist list
     
    Similar Threads
    Thread: Synchronized !
    Multidimensional ArrayList
    Default Object and String sizes?
    Classpath question
    Set.toArray