Prev: Java and awk (jawk)
Next: Possible easy diagnostic outputting from multiple threads tothe one text frame
From: Ross on 6 Jul 2010 11:30 Let's say that I have a java.util.ArrayList which will contain only strings. So, I can declare this as: ArrayList<String> blah = new ArrayList<String>(); So far so good, but let's say that blah is an array in itself. I'd expect to be able to do this: ArrayList<String> blah[] = new ArrayList<String>[ 50 ]; However, if I do that, I get a compile time error saying something about creating a generic array. If I modify the code to: ArrayList<String> blah[] = new ArrayList[ 50 ]; Then, it compiles and works, but gives me a warning about using a deprecated blah blah blah. What gives?
From: Lew on 6 Jul 2010 12:20 Ross wrote: > Let's say that I have a java.util.ArrayList which will contain only > strings. So, I can declare this as: > > ArrayList<String> blah = new ArrayList<String>(); > > So far so good, but let's say that blah is an array in itself. I'd > expect to be able to do this: > > ArrayList<String> blah[] = new ArrayList<String>[ 50 ]; > > However, if I do that, I get a compile time error saying something > about creating a generic array. If I modify the code to: > > ArrayList<String> blah[] = new ArrayList[ 50 ]; > > Then, it compiles and works, but gives me a warning about using a > deprecated blah blah blah. > > What gives? Arrays and generics don't mix. It has to do with arrays being reifiable but not generics. From the JLS, §10.10: "... creation of arrays of non-reifiable types is forbidden." <http://java.sun.com/docs/books/jls/third_edition/html/ arrays.html#10.10> -- Lew
From: markspace on 6 Jul 2010 12:23 It's a pain but Java's generics are not reifiable. Simple solution: prefer ArrayList to arrays: ArrayList<ArrayList<String>> arrayOfLists = new ArrayList<ArrayList<String>>( 50 ); arrayOfLists.add( new ArrayList<String>() ); etc.
From: Lew on 6 Jul 2010 12:29 markspace wrote: > It's a pain but Java's generics are not reifiable. Simple solution: > prefer ArrayList to arrays: > > ArrayList<ArrayList<String>> arrayOfLists = > new ArrayList<ArrayList<String>>( 50 ); > > arrayOfLists.add( new ArrayList<String>() ); > > etc. Prefer Lists of Lists to ArrayLists of ArrayLists: List <List <String>> holyOfHolies = new ArrayList <List <String>> (); holyOfHolies.add( new ArrayList <String> () ); -- Lew
From: Mike Schilling on 6 Jul 2010 13:18 "Lew" <lew(a)lewscanon.com> wrote in message news:f4a6bdb8-c41e-4978-9abf-70e5791f87f5(a)c33g2000yqm.googlegroups.com... > Ross wrote: >> Let's say that I have a java.util.ArrayList which will contain only >> strings. So, I can declare this as: >> >> ArrayList<String> blah = new ArrayList<String>(); >> >> So far so good, but let's say that blah is an array in itself. I'd >> expect to be able to do this: >> >> ArrayList<String> blah[] = new ArrayList<String>[ 50 ]; >> >> However, if I do that, I get a compile time error saying something >> about creating a generic array. If I modify the code to: >> >> ArrayList<String> blah[] = new ArrayList[ 50 ]; >> >> Then, it compiles and works, but gives me a warning about using a >> deprecated blah blah blah. >> >> What gives? > > Arrays and generics don't mix. It has to do with arrays being > reifiable but not generics. > > From the JLS, �10.10: > "... creation of arrays of non-reifiable types is forbidden." > <http://java.sun.com/docs/books/jls/third_edition/html/ > arrays.html#10.10> There aren't many instances in Java of "That's how it works. Don't ask why; you're better off not knowing", but this is one of them.
|
Next
|
Last
Pages: 1 2 3 Prev: Java and awk (jawk) Next: Possible easy diagnostic outputting from multiple threads tothe one text frame |