This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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
 
Saloon Keeper
Posts: 9145
173
  • 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
Saloon Keeper
Posts: 9145
173
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 21424
94
Chrome Eclipse IDE Java 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: 12199
199
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: 59786
188
  • 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: 12199
199
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
Saloon Keeper
Posts: 9145
173
  • 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: 59786
188
  • 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!