Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!.POSTED!not-for-mail From: Sebastian Newsgroups: comp.lang.java.programmer Subject: Re: Problem with generics and dynamic array copy Date: Fri, 22 Jul 2011 19:30:05 +0200 Organization: albasani.net Lines: 30 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.albasani.net YVLJnrDHUrwAfuc6JbpNtzn7dgU38Qi+sWF/ZnKm0VEAJ1zEwYowivj3kzPRaJGrp7UM2sDYsg7H5UHkd03yqA== NNTP-Posting-Date: Fri, 22 Jul 2011 17:30:06 +0000 (UTC) Injection-Info: news.albasani.net; logging-data="y2alVhbotEnxbwk5wB3QHi2Sfj60wzxpos+XID1suUiE+qostOR9Xz6eAdXcMqKCDj+5uIW8SpiPPKREWNaftFyoqlBhkcxI7vJSnSHLyltEG4upgiMQBm7NHrqq4P9W"; mail-complaints-to="abuse@albasani.net" User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 In-Reply-To: Cancel-Lock: sha1:oIZbR/eAwIfgRD8ujylaT9x/LqU= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:6406 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 to Class > > /** > * Copies the specified array to a new array with the same component > * type and length as the given array. > * @param the type of the array elements > * @param src the array to be copied > * @return the copy > */ > public static final T[] arraycopy( T[] src ) > { > Class 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. But why should that be necessary in view of the declaration of src? Is the thinking behind the method wrong? -- Sebastian