You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by pl...@netscape.net on 2002/08/14 21:19:15 UTC

Tool for Accessing Array Elements, and 2 Questions

Below is a bit of code I just came up with that lets you put a
multidimensional array in the context and access elements from it.

Example:  If you put in this method in a utility class, and add
the class and a two dimesional array to the context, then in your
velocity template you could write:

$util.getElement($myArray, [1,2])

to access myArray[1, 2].

Here's the getElement method:

   /**
    *  Returns a element from the given multidimensional array, using the
    *  integer indices in the given ArrayList.
    * @param array the array from which an element is to be retrieved
    * @param indices the indices to use in retrieving the element.  These
    *  should be Integer objects.
    * @return the element of array retrieved by the specified indices.  If
    *  the array has more dimensions than the number of given indices, the
    *  return value will be the subset of the array specified by the given
    *  indices.
    */
   public Object getElement(Object array, ArrayList indices) {
      Object rtn = array;
      int numIndices = indices.size();
      for (int i=0; rtn.getClass().isArray() && i<numIndices; ++i) {
         rtn = Array.get(rtn, ((Integer)indices.get(i)).intValue());
      }
      return rtn;
   }

And now for my two questions:

1) If this is so simple to do, why doesn't Velocity come with a
way to access array elements?

2) Could someone familiar with the reflection API comment on
performance issues?  For example, if I have a four dimensional
array with 10,000 elements, will accessing each element this way
be slow?  (And slow compared to what?)

Thanks,
   --Paul


__________________________________________________________________
Your favorite stores, helpful shopping tools and great gift ideas. Experience the convenience of buying online with Shop@Netscape! http://shopnow.netscape.com/

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Tool for Accessing Array Elements, and 2 Questions

Posted by Jeff Duska <Je...@noaa.gov>.
>
>
>And now for my two questions:
>
>1) If this is so simple to do, why doesn't Velocity come with a
>way to access array elements?
>
This is just my two cents worth, but it locks you into a data structure. 
Thus, you're view knows a lot about your implementation. If it turns out 
that a link list or hash map would better, you'll have to break you 
template to change. I try to make template know next to nothing about my 
Java code. It allows me to refactor my Java without breaking the templates.

I have no idea, if this why they did that. It just why I wouldn't do it. 
This is my personal preference right now. Tommorrow, I may like or see a 
need for arrays in VTL. Right now, I'd say create a helper object and 
put in your template.

HTH,

Jeff



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>