Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

dragdrop-colections

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
drag drop the appropriate choices so that the output is 121



Ans: toArray(new string[0]);

it is a drag drop from WHizlabs
could you please explain me .i couldn't understand anything how??
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This method returns an array containing all of the elements from the ArrayList. The array returned will have the same type as the argument passed to the method. In this case a String array is returned.

Note that I have added {} to the argument above so that an actual array is being passed and not an array declaration.
 
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. It works just like that.

Interface java.util.List has 2 toArray() methods.
One returns an array of type Object[] while the other returns an array of a 'specified type' (which is what we have here). You get the former if you do not pass in any parameters.

-If you pass an array of strings (i.e. new String[i]) into the toArray() method like this: toArray(new String[i]), you will get an array of strings.
-But, if you pass an array of say, dogs (i.e. new Dog[i]) into the toArray() method like this: toArray(new Dog[i]), you will get an array of dogs.

The interesting thing about doing this is that if the array you pass into the toArray() method is big enough to hold all the data in the list, you will get 'the same' array you passed in back (with null values at the end, if it is bigger). Otherwise you would get a new array with the exact size of the list.

So since List 'a' has 3 items and 'new String[0]' has zero items, you should get:
1. a string array (because you passed in a string array)
2. a 'new' string array with 3 items (because 0 items are less than 3 items).

...And note. toArray(new String[0]) works. toArray(new String[0]{}) does not work. toArray(new String[]{}), however, works.

-Read the API, and try out toArray(new String[4]) and see what you get.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic