Iterate arrays of different types

J

Jon Rasmussen

I have multiple arrays:

TransactionSet[]
FunctionSet[]
AssetSet[]

All these objects have methods:
getCode()
getName()


I want to create a generic method that can iterate the arrays and call the getters.

public void iterateArray(Object[] o)
{
[process]
}


Is this possible / how can I do this in Java?

Thanks,

Jon
 
T

Thomas G. Marshall

Jon Rasmussen said:
I have multiple arrays:

TransactionSet[]
FunctionSet[]
AssetSet[]

All these objects have methods:
getCode()
getName()


I want to create a generic method that can iterate the arrays and
call the getters.

public void iterateArray(Object[] o)
{
[process]
}


Is this possible / how can I do this in Java?

Thanks,

Jon

public interface GetterInterface
{
public Code getCode();
public Name getName();
}

class Transaction implements GetterInterface
....
class Function implements GetterInterface
....
class Asset implements GetterInterface
....

// "set" is bad term
Transaction[] transactionSet = .....
Function[] functionSet = .....
Asset[] assetSet =...


public void iterateArray(GetterInterface[] gi_array)
{
// loop any way you want...
for (int i=0; i<gi_array.length; i++)
{
/something/ = gi_array.getCode();
/something/ = gi_array.getName();
}
}
 
J

Jacob

Thomas said:
public interface GetterInterface
{
public Code getCode();
public Name getName();
}

class Transaction implements GetterInterface
...
class Function implements GetterInterface
...
class Asset implements GetterInterface
...

This is technically correct, but there might be reasons
you don't want this. Such as:

a) You don't control the source code for Transaction, etc.
b) The three don't really share this property ("GetterInterface")
as such, it is more of technical side effect of name matching.

If this is the case, I'd prefer introspection to solve
the problem (find the Objects methods, call the required one).
This obviously makes the loop dirtier, but it leaves
the given objects alone as is.

Of course: Beware of lack of robustness to change as with
most introspection depending code.
 
T

Thomas G. Marshall

Jacob said:
This is technically correct, but there might be reasons
you don't want this. Such as:

a) You don't control the source code for Transaction, etc.
b) The three don't really share this property ("GetterInterface")
as such, it is more of technical side effect of name matching.

It's pretty clear to me that this is /his/ source that we're talking about,
but whatever...

If this is the case, I'd prefer introspection to solve
the problem (find the Objects methods, call the required one).
This obviously makes the loop dirtier, but it leaves
the given objects alone as is.

Of course: Beware of lack of robustness to change as with
most introspection depending code.

I've used instrospection/reflection to a very large degree in Java. Be
/very/ careful when you attempt this----many sharks in that pool just
waiting to swim up and bite you in the ass later on. Most of them start
with M and rhyme with /zaintainability/.
 
C

Chris

If you control the code, then I suggest you refactor as one previous
poster suggested (by deriving each array from a common interface), or
even better by creating a class containing a transaction, function and
asset and iterate through an array of that class.

If you don't control the code, then I suggest you create an adapter
class that encapsulates the three separate arrays and provides an
iterator, something along the lines of:

class Entity
{
Transaction transaction;
Asset asset;
...
}

class Adapter
{
Transaction[] transactions ...
Asset[] assets ...
// etc

// Or even better, use an Iterator
public Entity get( int index )
{
return new Entity( transaction[ index ], assets[ index ] ... )
}
}

- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top