• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Problem with Generic Array Creation  RSS feed

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would expect the following code to compile:


However, it does not. The error message is: error: generic array creation
The problem is with the line that starts with: this.coeff =
Is there a problem creating generic arrays in Java?

Bob
 
Bartender
Posts: 9495
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, you're not allowed to create generic arrays in Java. The reason is that arrays are covariant, and generic types are invariant by default. If the language allowed generic array creation, type safety would break.

You solve this by either using a collection type internally, or an object array and then casting the elements to T. Using a collection type definitely is the better choice.
 
Stephan van Hulst
Bartender
Posts: 9495
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the following looks slightly better, because technically you should have used a collector to transform the stream into a list.
 
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bob Sherry wrote:I would expect the following code to compile:

However, it does not.


This will work:


 
Bob Sherry
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You suggested this:

This does not seem safe to me because I think of this.coeff as a pointer and setting it to arguments that are on the stack will cause problems when the method returns. Are you sure it is safe?
Bob
 
Marshal
Posts: 61773
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, this.coeff is a field hiding a reference; it isn't a pointer.
That isn't a method; if it compiles it is a constructor.
Yes, you can have problems because you are passing a reference to a mutable reference type (that array) into the constructor; you should take a defensive copy:-You cannot create a generic array; the compiler never likes new T[...]
Minor style thing: the [] operator is applied to the type T not the identifier (variable name): T[] coeff please, not T []coeff Similarly for the ... in the parameter.
And coeff is probably not a good variable name because it is an abbreviation and probably doesn't refer to any coefficients.
 
Junilu Lacar
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're going to pass in an array instead of a variable list of parameters to a method/constructor that declares (T... coeff), then yes, you're going to need to make a defensive copy. However, if you pass in a variable argument list, nobody can touch that. At any rate, you can do this if you want to make a defensive copy on entry to your object:

And if you want to hand out defensive copies, you do pretty much the same thing:
 
Stephan van Hulst
Bartender
Posts: 9495
184
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:I think the following looks slightly better, because technically you should have used a collector to transform the stream into a list.


You're right, it had been a long day for me. I think I actually would have done the following:
 
Campbell Ritchie
Marshal
Posts: 61773
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaaaaaaa! If you call the class Polynomial it becomes clear that the numbers do in fact represent coefficients.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!