You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Matthew Van Horn <ma...@yahoo.com> on 2004/05/08 14:44:03 UTC

arrays and other indexed collections

What is the 'right' way to access a particular element of an collection?
I may have missed something obvious, as I am sleepy, but I can't see how
to do this without a lot of unecessary looping.

as an example, I have objects like this in my context
public class Foo {
	List bars;
	String[] baz;
	...
}
public class Bar {
	String name;
	...
}

I want to show a table with a row for each Foo, and a cell showing the
name of the Foo's first Bar, and a cell with the first baz
in Java this would be:

myFoo.getBars().get(0).getName()
myFoo.getBaz()[0];

but I am not sure how to do this in velocity, especially if I want to
use an index other than 0. 	

Thank for any help,
-- 
Matthew Van Horn <ma...@yahoo.com>


---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org


Re: arrays and other indexed collections

Posted by Nathan Bubna <na...@esha.com>.
Matthew Van Horn said:
> What is the 'right' way to access a particular element of an collection?
> I may have missed something obvious, as I am sleepy, but I can't see how
> to do this without a lot of unecessary looping.
...
> in Java this would be:
> 
> myFoo.getBars().get(0).getName()
> myFoo.getBaz()[0];
> 
> but I am not sure how to do this in velocity, especially if I want to
> use an index other than 0. 

in VTL this would be:

$myFoo.bars.get($someIntegerOtherThanZero).name
$myArrayTool.getElement($sIOTZ, $myFoo.baz)

or else amend your java to make "getBaz()" return a List.

Nathan Bubna
nathan@esha.com

---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-user-help@jakarta.apache.org