Groups | Search | Server Info | Keyboard shortcuts | Login | Register


Groups > comp.lang.java.programmer > #6406

Re: Problem with generics and dynamic array copy

From Sebastian <sebastian@undisclosed.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Problem with generics and dynamic array copy
Date 2011-07-22 19:30 +0200
Organization albasani.net
Message-ID <j0cc2t$289$2@news.albasani.net> (permalink)
References <j0cbru$289$1@news.albasani.net>

Show all headers | View raw


Am 22.07.2011 19:26, schrieb Sebastian:
> Hello there,
>
> how can I make the compile error go away? The marked line is wrong:
> Type mismatch: cannot convert from Class<capture#2-of ?> to Class<T>
>
> /**
> * Copies the specified array to a new array with the same component
> * type and length as the given array.
> * @param <T> the type of the array elements
> * @param src the array to be copied
> * @return the copy
> */
> public static final <T> T[] arraycopy( T[] src )
> {
> Class<T> componentType = src.getClass().getComponentType(); // !!!!
> @SuppressWarnings("unchecked")
> T[] dest = (T[]) Array.newInstance( componentType, src.length );
> System.arraycopy( src, 0, dest, 0, src.length );
> return dest;
> }
>
> I'd be grateful for a hint.
> -- Sebastian

to be mor exact: of course I could just cast componentType to Class<T>.
But why should that be necessary in view of the declaration of src?
Is the thinking behind the method wrong?

-- Sebastian

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 19:26 +0200
  Re: Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 19:30 +0200
    Re: Problem with generics and dynamic array copy markspace <-@.> - 2011-07-22 11:23 -0700
      Re: Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 20:36 +0200
        Re: Problem with generics and dynamic array copy Robert Klemme <shortcutter@googlemail.com> - 2011-07-22 21:27 +0200
          Re: Problem with generics and dynamic array copy lewbloch <lewbloch@gmail.com> - 2011-07-22 13:33 -0700
          Re: Problem with generics and dynamic array copy Sebastian <sebastian@undisclosed.invalid> - 2011-07-22 22:37 +0200
            Re: Problem with generics and dynamic array copy lewbloch <lewbloch@gmail.com> - 2011-07-22 15:49 -0700

csiph-web