Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


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

Re: Problem with generics and dynamic array copy

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!news.glorb.com!postnews.google.com!d8g2000prf.googlegroups.com!not-for-mail
From lewbloch <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Problem with generics and dynamic array copy
Date Fri, 22 Jul 2011 15:49:04 -0700 (PDT)
Organization http://groups.google.com
Lines 72
Message-ID <8d967dd6-a043-4e28-a9a3-6a158791ecba@d8g2000prf.googlegroups.com> (permalink)
References <j0cbru$289$1@news.albasani.net> <j0cc2t$289$2@news.albasani.net> <j0cf75$8pm$1@dont-email.me> <j0cfva$cmc$1@news.albasani.net> <98u18jF6l7U1@mid.individual.net> <j0cn2k$u6p$1@news.albasani.net>
NNTP-Posting-Host 127.6.6.6
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1311375030 13027 127.0.0.1 (22 Jul 2011 22:50:30 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Fri, 22 Jul 2011 22:50:30 +0000 (UTC)
Complaints-To groups-abuse@google.com
Injection-Info d8g2000prf.googlegroups.com; posting-host=127.6.6.6; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-Header-Order ASELCHRU
X-HTTP-UserAgent Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.99 Safari/535.1,gzip(gfe)
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6432

Show key headers only | View raw


On Jul 22, 1:37 pm, Sebastian <sebast...@undisclosed.invalid> wrote:
> Am 22.07.2011 21:27, schrieb Robert Klemme:
>
>
>
>
>
>
>
> > On 22.07.2011 20:36, Sebastian wrote:
> >> Am 22.07.2011 20:23, schrieb markspace:
> >>> On 7/22/2011 10:30 AM, Sebastian wrote:
>
> >>>>> public static final <T> T[] arraycopy( T[] src )
> >>>>> {
> >>>>> Class<T> componentType = src.getClass().getComponentType(); // !!!!
>
> >>> RTFM. getComponentType() returns Class<?>, not Class<T>.
>
> >>> <http://download.oracle.com/javase/6/docs/api/java/lang/Class.html#get...>
>
> >>> public Class<?> getComponentType()
>
> >>> Returns the Class representing the component type of an array. If this
> >>> class does not represent an array class this method returns null.
>
> >> well, yes, but given that src.getClass() must give one the class object
> >> for arrays with component type T, why is the compiler not smart enough
> >> to infer that the unknown class parameter in the return value of
> >> getComponentType() must be T? As a human I can see that, that's why I
> >> can cast to Class<T>, but I don't believe that I'm smarter than javac...
>
> > The compiler has no idea what the semantics of getComponentType() is. It
> > could be implemented as
>
> > public Class<?> getComponentType() { return Object.class; }
>
> > and still be conformant to the declaration. Hence it cannot do any
> > automatic inference based on the fact you know that the array is T[].
> > Btw, you can actually pass B[] where B is a subclass of T.
>
> > Since Array.newInstance() accepts Class<?> you should simply use that -
> > that cast to T[] is needed anyway.
>
> > Of course, even better you scrap your implementation and use
>
> >http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#co...[],%20int%29
>
> > Cheers
>
> > robert
>
> Thanks. Java has a confusing variety of ways to copy an array:
>   - the copyOf and copyOfRange methods in Arrays
>   - System.arraycopy
>   - clone and do a cast
>
> I'll go with your recommendation.

'clone()' and cast is the least optimal.  'System.arraycopy()' is the
"old" way.  'Arrays.copyOf()' is the "new" way and most typesafe, and
is somewhat easier to use than the others.

--
Lew

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


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