Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generics in Java 1.5 ( or is it java 5.0 ?... I always haveconfusion)

Reply
Thread Tools

Generics in Java 1.5 ( or is it java 5.0 ?... I always haveconfusion)

 
 
Vikram
Guest
Posts: n/a
 
      06-12-2008
Hi,
Looking at the signature of the following method in
java.util.List

public interface List<E> extends Collection<E> {
.........
........
......

<T> T[] toArray(T[] a);
}

I wrote a small program as below:

ArrayList<String> c = new ArrayList<String>();
c.add("Vikram");
c.add("Pyati");
Integer[] i = new Integer[20];
c.toArray(i);

This did not give me a compilation error, though it fails at runtime
giving java.lang.ArrayStoreException, which is perfect.

My question is , why did the above mentioned method be declared as
<E> E[] toArray(E[] a);
which will force the method to take only the array of formal type ( in
this case a String[] ) at the compile time
 
Reply With Quote
 
 
 
 
Roland de Ruiter
Guest
Posts: n/a
 
      06-12-2008
On 12-6-2008 10:37, Vikram wrote:
> Hi,
> Looking at the signature of the following method in
> java.util.List
>
> public interface List<E> extends Collection<E> {
> ........
> .......
> .....
>
> <T> T[] toArray(T[] a);
> }
>
> I wrote a small program as below:
>
> ArrayList<String> c = new ArrayList<String>();
> c.add("Vikram");
> c.add("Pyati");
> Integer[] i = new Integer[20];
> c.toArray(i);
>
> This did not give me a compilation error, though it fails at runtime
> giving java.lang.ArrayStoreException, which is perfect.
>
> My question is , why did the above mentioned method be declared as
> <E> E[] toArray(E[] a);


You probably mean
E[] toArray(E[] a);
without the formal type parameter. Now E refers to the type parameter E
of class List.

> which will force the method to take only the array of formal type ( in
> this case a String[] ) at the compile time


In that case you could only convert the list to an array of E, and not
to an array of a superclass / interface of E.

There may be cases where it's useful to convert a list to an array of
Objects. With your proposal that's not possible (it would generating a
compile time error).

Modified example:

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


public class ToArray {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("Vikram");
list.add("Pyati");

// The following is OK
Object[] arrO = new Object[list.size()];
Object[] resArrO = list.toArray(arrO);
System.out.println(Arrays.deepToString(resArrO));

// The following is OK; String implements Serializable
Serializable[] arrS = new Serializable[list.size()];
Serializable[] resArrS = list.toArray(arrS);
System.out.println(Arrays.deepToString(resArrS));

// The following is OK at compile time, but toArray causes an
// ArrayStoreException at runtime
Integer[] arrI = new Integer[list.size()];
Integer[] resArrI = list.toArray(arrI);
System.out.println(Arrays.deepToString(resArrI));
}
}





--
Regards,

Roland
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-12-2008
Vikram wrote:
> Hi,
> Looking at the signature of the following method in
> java.util.List
>
> public interface List<E> extends Collection<E> {
> .........
> ........
> ......
>
> <T> T[] toArray(T[] a);
> }
>
> I wrote a small program as below:
>
> ArrayList<String> c = new ArrayList<String>();
> c.add("Vikram");
> c.add("Pyati");
> Integer[] i = new Integer[20];
> c.toArray(i);
>
> This did not give me a compilation error, though it fails at runtime
> giving java.lang.ArrayStoreException, which is perfect.
>
> My question is , why did the above mentioned method be declared as
> <E> E[] toArray(E[] a);
> which will force the method to take only the array of formal type ( in
> this case a String[] ) at the compile time

So, first off, Generics and Arrays don't always mix well.

Second, I think you're attempting to express this:
<T super E> toArray(T[] a);

Although, that breaks this:
List<Number> c = new ArrayList<Number>();
c.add(new Integer(3));
c.add(new Integer(15));

Integer[] i = c.toArray(new Integer[0]);

the toArray(Object[]) method is inherently a run-time only method,
because it can be used to create any type of array.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
 
Reply With Quote
 
d
Guest
Posts: n/a
 
      06-12-2008
> > * * * Object[] arrO = new Object[list.size()];
> > * * * Object[] resArrO = list.toArray(arrO);

>
> Alternative idiom, purely a matter of style as the net result is the same:
>
> * Object[] resArrO = list.toArray( new Object[0] );
>
> I point this out not to recommend it, although personally I like it, but to
> alert those who may run across it from time to time.


I much prefer the 2nd example simply because you do not need the arr0
reference (which to me is syntactic clutter).
 
Reply With Quote
 
Vikram
Guest
Posts: n/a
 
      06-13-2008
On Jun 12, 8:42*pm, Lew <(E-Mail Removed)> wrote:
> d wrote:
> >>> * * * Object[] arrO = new Object[list.size()];
> >>> * * * Object[] resArrO = list.toArray(arrO);
> >> Alternative idiom, purely a matter of style as the net result is the same:

>
> >> * Object[] resArrO = list.toArray( new Object[0] );

>
> >> I point this out not to recommend it, although personally I like it, but to
> >> alert those who may run across it from time to time.

>
> > I much prefer the 2nd example simply because you do not need the arr0
> > reference (which to me is syntactic clutter).

>
> You don't need it in the first idiom, really:
>
> * *Object[] resArrO = list.toArray( new Object[list.size()] );
>
> Difference eliminated.
>
> --
> Lew


Thanks very much to everyone... Now I am clear
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
Any tool to convert java raw code (a la java 1.4) into generics code Royan Java 8 02-15-2008 02:35 PM
Java Regular Expression (java.util.regex ): Multiple Occurences, always guaranteed that it takes the last? joes Java 2 05-25-2007 05:57 PM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM
Trying to create a CSS box that is always is always the width of an image placed inside it (and no wider) Deryck HTML 4 06-22-2004 08:25 PM



Advertisments