• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with Generic Array Creation

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaaaaaaaaaaaaaaaaaaaaaaaaaaa! If you call the class Polynomial it becomes clear that the numbers do in fact represent coefficients.
 
reply
    Bookmark Topic Watch Topic
  • New Topic