Re: Customize toString() for arrays...?
On Fri, 23 Jul 2010, markspace wrote:
Tom Anderson wrote:
Yes, in this case, you are largely stuffed. UNLESS you pay heed to my
super-wily suggestion:
...
This only works with arrays of objects, but you could write a family of
specialised versions for arrays of primitives, and create the appropriate
one in the getter.
I thought my autoBox() method was more wily, because it dealt with
primitives.
You deal with primitives, but not nested lists, which is where i was
applying wiliness. We are disjointly wily. But we can unionise: in my
wrapper, change the type of the array instance variable to Object, and
rewrite the getter to use your reflective approach:
Object object = Array.get(array, index);
if (obj.getClass().isArray()) object = new ArrayWrapper(object);
return object;
Now we have something that handles primitives and nested lists, and does
so with minimal allocation (just small wrapper objects, never arrays), and
doesn't require a type parameter to be passed in.
It's not as generally useful as your unbox method could be, because it
isn't typesafe, but it will do for toString.
tom
--
Pool's closed.