• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

problem with collections

 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object[] toArray()
Object[] toArray(Object a[])


If the given array is big enough, the elements are stored in this array. If there is room to spare in the array, that is, the length of the array is greater than the number of elements in the collection, the spare room is filled with null values before the array is returned. If the array is too small, a new array of the same runtime type and appropriate size is created. If the runtime type of the specified array is not a supertype of the runtime type of every element in the collection, an ArrayStoreException is thrown.

Based up on the above statement i tried the below program

import java.util.*;
class A
{

public static void main(String... args)
{
ArrayList al=new ArrayList();
ArrayList al1=new ArrayList();
al.add("anil");
al.add("kumar");
al.add(3);
al1.addAll(al);
al.add(4);
Object[] a= new Object[10];
//a[0]="lalam";//line 1
//a[1]="Raj";//line 2
for(Object a1:a)
System.out.println("The value is : "+a1);
a=al.toArray();
System.out.println("The length after toArray is :"+a.length);
for(Object s:a)
System.out.println("The value in the String array is : "+s);
}
}

i got this output:

C:\practice>java A
The length is of a is : 10
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
//line 3
The value in the String array is : anil
The value in the String array is : kumar
The value in the String array is : 3
The value in the String array is : 4

line 4
---------------------------------------------------------------------------
the length of the array is greater than the number of elements in the collection, the spare room is filled with null values before the array is returned.
-----------------------------------------------------------------------
According to the above statements the output should be after line 3

The value in the String array is : anil
The value in the String array is : kumar
The value in the String array is : 3
The value in the String array is : 4

followed by 6 null 's
ok
Now i uncommented the lines 1 and 2

Now i got this output
C:\practice>java A
The length is of a is : 10
The value is : lalam
The value is : Raj
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value is : null
The value in the String array is : anil
The value in the String array is : kumar
The value in the String array is : 3
The value in the String array is : 4

Now i have understood that at run time the new reference of new Object[] is
being assigned to Object a[].And the eariler one my be garbage collected.

But my Question is what is the meaning of line 4
What does the author wants to say?
is there any thing i am missing?
please tell me.
[ April 01, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line 4 is correct.
It applies to the method toArray(T a[]), but it does not apply to toArray().

In your code you are using
a=al.toArray();

Instead of that, just try
a=al.toArray(a);

You will get what you want
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Ganesha Kumar

i have seen that i got that.But for first method Object[] toArray().Is there any thing the author wants to say .
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai
Can you please explain me the problem and solution .Iam unable to follow the solution
Thanks
 
Ganesha Kumar
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per the Sun API documentation,
toArray() method returns an array with the elements from the collection.
The array and the collection are not linked to one another. That is, if
you change the array, it does not affect the list. That's it.
 
This parrot is no more. It has ceased to be. Now it's a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic