aspose file tools*
The moose likes Beginning Java and the fly likes basic use of generic arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "basic use of generic arrays" Watch "basic use of generic arrays" New topic
Author

basic use of generic arrays

John Wickerson
Greenhorn

Joined: Mar 16, 2006
Posts: 13
Hey guys,

I have the following code, which I'm using to read each element from an array in turn. I've recently modified it to work with an array of any type, but it no longer works.



When I use it like this, I am told that the constructor ArrayReader<Character>(char[]) is undefined.



What's going on here? Thanks v much for any help....

Kind regards,
John
[ March 28, 2006: Message edited by: John Wickerson ]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You are bumping up against a basic limitation of generic types in Java: T doesn't range over primitive types like char. To make your example work, you have to copy your char[] into a Character[]. I don't know any method to do that, so just roll up your sleeves and write a loop.


There is no emoticon for what I am feeling!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Originally posted by Jeff Albertson:
You are bumping up against a basic limitation of generic types in Java: T doesn't range over primitive types like char. To make your example work, you have to copy your char[] into a Character[]. I don't know any method to do that, so just roll up your sleeves and write a loop.


Indeed, this is a horrible limitation, however, generics are not the cause. The existence of both primitive types and arrays is the cause, and as a consequence, generics are unable to achieve this common use case due to the leak of abstraction.

ContractualJ attempts to work around the whole mess of primitives with the net.tmorris.primitives package and the mess of arrays with net.tmorris.sequence. The solution is partially optimal, but as optimal as possible within the constraints of the assumed context (e.g. must compile with a Java compiler is a devastational constraint but one that I choose to impose).

I continually find it astonishing that so many subscribe to the legitimacy of these constructs, albeit suffering consequences of this blind faith often stated at the same time!! ...quite often attribution of the cause to a consequence of the cause, instead of externalising the issue for analysis and deriving the real cause (which sometimes contradicts the belief of the establishment).


Tony Morris
Java Q&A (FAQ, Trivia)
John Wickerson
Greenhorn

Joined: Mar 16, 2006
Posts: 13
Thanks Jeff and Tony. I must admit it's quite a satisfying feeling when I finds that my question does not have a blindingly obvious answer that I should have seen. Since I'm only using the ArrayReader with chars at the moment, I guess I'll fix it to become a CharArrayReader instead.

Regards,
John
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: basic use of generic arrays