• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Generics Question

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following code?


Source:- http://www.examulator.com/phezam/
 
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

System.out.print(sFruit); //how come this prints orangemango only



i'm getting orange apple orange mango

first orange from

System.out.print(saFruit[1]);



what does new String[0] contains???

i dont know
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what does new String[0] contains???



The array that was passed is an empty array that the toArray() method will use only to determine the type (because the result to be return couldn't fit). In this example, a new array of the same type, but larger, is returned.

Henry
 
Chiranjeevi Kanthraj
Ranch Hand
Posts: 290
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what does new String[0] contains???



Which tells the toArray() method to give in String array format
String[0] means you telling to put in the String array with 0 elements, but ArrayList has 3 elements so it will append to the String[0].
If you need to see the difference make that String[0] to more the 3 means more then elements which ArrayList has you will get like below

means it will add null to left over array elements.

i think i catch this.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
al.toArray(new String[0]) works like al.toArray() function if the list contain only String objects
 
Henry Wong
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Chiranjeevi Kanthraj wrote:
String[0] means you telling to put in the String array with 0 elements, but ArrayList has 3 elements so it will append to the String[0].



Actually, no. It won't append to the array -- Java arrays can't change in size. The method will instantiate a new array of the same type.

Chiranjeevi Kanthraj wrote:
If you need to see the difference make that String[0] to more the 3 means more then elements which ArrayList has you will get like below

means it will add null to left over array elements.



When you pass in an array that can fit the data -- it will simply use it, and ignore any leftover array elements. The reason the left over array elements were null, is because they were null when you intantiated the array. The toArray() method didn't add any nulls.

Henry
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic