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


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

Re: Cannot create a generic array of <type>

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!nx02.iad01.newshosting.com!newshosting.com!69.16.185.21.MISMATCH!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
From Lew <lewbloch@gmail.com>
Newsgroups comp.lang.java.programmer
Subject Re: Cannot create a generic array of <type>
Date Tue, 18 Oct 2011 23:28:48 -0700 (PDT)
Organization http://groups.google.com
Lines 44
Message-ID <11633107.472.1319005728890.JavaMail.geo-discussion-forums@prfp13> (permalink)
References <j7lic6$lob$1@speranza.aioe.org>
Reply-To comp.lang.java.programmer@googlegroups.com
NNTP-Posting-Host 173.164.137.214
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-Trace posting.google.com 1319005729 23671 127.0.0.1 (19 Oct 2011 06:28:49 GMT)
X-Complaints-To groups-abuse@google.com
NNTP-Posting-Date Wed, 19 Oct 2011 06:28:49 +0000 (UTC)
In-Reply-To <j7lic6$lob$1@speranza.aioe.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=173.164.137.214; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T
User-Agent G2/1.0
X-Google-Web-Client true
Xref x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8977

Show key headers only | View raw


Warren Tang wrote:
> The following code:
> 
>   ArrayList<Integer>[] indexedValues = new ArrayList<Integer>[5];
> 
> generates an error:
> 
>   Cannot create a generic array of ArrayList<Integer>
> 
> Could someone explain this to me?

Sure.  The short answer is that generics and arrays do not mix.  Slightly longer, the Java Language Specification explicitly forbids arrays of non-reifiable types:

"An array creation expression specifies the element type, the number of levels of nested arrays, and the length of the array for at least one of the levels of nesting. ... It is a compile-time error if the element type is not a reifiable type".
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.3>

Generic types like 'ArrayList<Integer>' are not reifiable, that is, you cannot actually create such a type at runtime due to type erasure.

The longer answer is that arrays are reifiable, therefore their element types must also be reifiable.  Unlike generics, arrays carry their base type with them at run time - they "know" their type.  Generic types do not.  (You can get around this with a run-time type token, or RTTT, but that's a topic for another discussion.)  So when you try to make an array of a generic type you deprive the array of information it needs.  This the compiler won't allow.

Back to the short answer: don't mix arrays and generics.  There are a few conversion classes in the Collections framework that let you convert between them, but use them sparingly and usually to go from array-land to collections-land to stay.

Arrays.asList
static <T> List<T> asList(T... a)
<http://download.oracle.com/javase/7/docs/api/java/util/Arrays.html#asList(T...)>

-- 
Lew

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


Thread

Cannot create a generic array of <type> Warren Tang <nospam@tangcs.com> - 2011-10-19 12:06 +0800
  Re: Cannot create a generic array of <type> Warren Tang <nospam@tangcs.com> - 2011-10-19 13:22 +0800
  Re: Cannot create a generic array of <type> Lew <lewbloch@gmail.com> - 2011-10-18 23:28 -0700
    Re: Cannot create a generic array of <type> Warren Tang <nospam@tangcs.com> - 2011-10-19 18:11 +0800
      Re: Cannot create a generic array of <type> markspace <-@.> - 2011-10-19 04:50 -0700
        Re: Cannot create a generic array of <type> Warren Tang <nospam@tangcs.com> - 2011-10-20 00:43 +0800
          Re: Cannot create a generic array of <type> markspace <-@.> - 2011-10-19 12:21 -0700
            Re: Cannot create a generic array of <type> Warren Tang <nospam@tangcs.com> - 2011-10-21 11:05 +0800
      Re: Cannot create a generic array of <type> Lew <lewbloch@gmail.com> - 2011-10-19 10:00 -0700

csiph-web