Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Making array classes

Reply
Thread Tools

Making array classes

 
 
Tom Anderson
Guest
Posts: n/a
 
      08-25-2003
If i have a class, is there some way i can make a class for the
corresponding array? To clarify, i'm after an implementation for:

void makeArrayClass( Class cl )

Such that

makeArrayClass( cl ).getComponentType().equals( cl )

So that i can do, for example:

import java.lang.reflect.Array ;
void example( Class cl )
{
Class arrayCl = makeArrayClass( cl ) ;
Object[][] array = (Object[][])Array.newInstance( arrayCl, 3 ) ;
array[0] = Array.newInstance( cl, 3 ) ;
array[0][0] = cl.newInstance()
}

My end goal is to make arrays of arbitrary dimension and size (and they
may be ragged, so i can't use Array.newInstance(Class,int[])).

I've poked about in Class and Array, but can't see anything.

TIA and hello to all,
tom

--
the logical extension of a zero-infinity nightmare topology

 
Reply With Quote
 
 
 
 
Wolfram Rittmeyer
Guest
Posts: n/a
 
      08-25-2003
Tom Anderson wrote:
> If i have a class, is there some way i can make a class for the
> corresponding array? To clarify, i'm after an implementation for:
>
> void makeArrayClass( Class cl )
>
> Such that
>
> makeArrayClass( cl ).getComponentType().equals( cl )
>
> So that i can do, for example:
>
> import java.lang.reflect.Array ;
> void example( Class cl )
> {
> Class arrayCl = makeArrayClass( cl ) ;
> Object[][] array = (Object[][])Array.newInstance( arrayCl, 3 ) ;
> array[0] = Array.newInstance( cl, 3 ) ;
> array[0][0] = cl.newInstance()
> }
>
> My end goal is to make arrays of arbitrary dimension and size (and they
> may be ragged, so i can't use Array.newInstance(Class,int[])).
>
> I've poked about in Class and Array, but can't see anything.
>
> TIA and hello to all,
> tom
>


No, there's not. You can construct an Object-array and pass your objects
into it, but that's not typesafe. With generics of JDK 1.5 you should be
able to do something like that, but that's most probably not released
before June 2004. BTW: are you sure that ArrayList or something similar
does not fit your needs better (problem with type-safety remains though)?



 
Reply With Quote
 
 
 
 
Tom Anderson
Guest
Posts: n/a
 
      08-25-2003
On Mon, 25 Aug 2003, Wolfram Rittmeyer wrote:

> Tom Anderson wrote:
>
> > If i have a class, is there some way i can make a class for the
> > corresponding array?


Incidentally, the two hacks i've thought of (but not tested) so far are:

- Munge the class name; for class Foo, "[LFoo;" is its array class,
"[[LFoo;" its array-of-array class, etc. You can pass these strings to
Class.forName to get the actual class objects.

- Abuse java.lang.reflect.Array's newInstance(Class,int[]) method; by
passing in the base class, then an array of the right length with zeroes
in all but the first place, you can make an array of the right type
which happens to be filled with zero-length arrays (rather than the
nulls you'd get if you constructed it directly).

> No, there's not.


Curses!

> You can construct an Object-array and pass your objects into it, but
> that's not typesafe. With generics of JDK 1.5 you should be able to do
> something like that, but that's most probably not released before June
> 2004.


Tiger's generics are only skin-deep: they're handled by type erasure,
which means you can't do:

<T> T[] f()
{
return new T[3] ;
}

Or if you can, what you actually get is an Object[] decorated to look like
a T[].

> BTW: are you sure that ArrayList or something similar does not fit your
> needs better (problem with type-safety remains though)?


I'm afraid nothing less than real live arrays will do; i'm writing some
generic quasi-reflective code for [de]serializing objects (purely for my
own amusement and education), and it has to be able to handle client code
throwing more or less arbitrary objects, including ragged multidimensional
arrays, at it.

tom

--
Brace yourself for an engulfing, cowardly autotroph! I want your photosynthetic apparatii!

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      08-25-2003
Tom Anderson wrote:
> If i have a class, is there some way i can make a class for the
> corresponding array?


You may want to check out:

http://groups.google.com/groups?thre...ta.cs.uiuc.edu

which discussed just this question.

-- chris


 
Reply With Quote
 
Tom Anderson
Guest
Posts: n/a
 
      08-28-2003
On Mon, 25 Aug 2003, John C. Bollinger wrote:

> Chris Uppal wrote:
> > Tom Anderson wrote:
> >
> >>If i have a class, is there some way i can make a class for the
> >>corresponding array?

>
> (1) Array classes describe the component type and number of dimensions,
> but not the size of any dimension. (I think the OP already realized this.)


Indeed i did.

> (2) An array class constructed with an array-type component A and n
> dimensions is the same as the class with the component type of A and n +
> (dumber of A's dimensions) dimensions. [Apply recursively if you like.]
>
> (3) If you don't know the component type and/or number of dimensions at
> compile-time then any such class or object you construct will have to be
> handled either generically or reflectively.
>
> With those in mind, one way to proceed is to construct the array you
> want from the bottom up: take the base objects and assemble them into 1D
> arrays in whatever arrangement you want, using Array.newInstance(Class,
> int); then take the 1D arrays and assemble them into 1D arrays, again
> using Array.newInstance(Class, int), resulting in 2D arrays or the base
> component type; continue in the same vein until you have one n-D array
> containing everything. You can make an array of arbitrary base
> component type and arbitrary shape (subject to VM limits) in this way.


Sadly, that scheme won't work for me - i really need to have the outer
arrays first, to put the inner arrays into. Well, i could change my code
to work that way round, but i'd rather not if i can avoid it.

However, your idea of recursively building up the type is quite cunning; i
can write makeArrayClass like this:

Class makeArrayClass( Class base, int dims )
{
Class cl = base ;
for (int i = 0 ; i < (dims - 1) ; ++i)
{
cl = Array.newInstance( base, 0 ).getClass() ;
}
return cl ;
}

And then pass the result into Array.newInstance to get the real thing.

Better yet, i can start off by building myself a little stash of array
classes like Russian dolls, so i don't have to repeat the computation at
every level.

Anyway, it seems that There Is More Than One Way To Do It. How perfectly
disgusting!

Thanks for your help.

tom

--
Suddenly, everything is clear ...

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: A fresh new approach to making making online Fakename Computer Information 0 11-18-2005 02:10 AM
Re: Definitive documentation on newstyle classes? (WAS: Picklingand inheritance are making me hurt) Brian van den Broek Python 4 02-09-2005 02:47 PM
Definitive documentation on newstyle classes? (WAS: Pickling andinheritance are making me hurt) Daniel Bickett Python 2 02-06-2005 01:39 AM
Making new Flavors : Making a custom transferhandler for and drop applications ebby83@gmail.com Java 5 01-12-2005 11:10 AM
Making classes from Metaclasses globally available Jean-François Doyon Python 13 06-17-2004 08:17 AM



Advertisments