Whoops, d'oh! Right, array.length is different from list.size().
Okay, one more question. Here's a real-life situation from a piece of code I have recently written (but which I am not averse to refactoring). I've altered the problem domain to protect the innocent.
I have two kinds of things, call them Nodes and
Patterns, in my database. Patterns and Nodes are composed of several descriptive fields, each of which can have one or more aggregation levels (e.g. New York City - USA - World). For the sake of the example, say the fields are eye color, gender, and city. A node is a specific thing, like "blue-eyed woman in New York City" or "brown-eyed man in Atlanta". Patterns can be wildcarded in zero or more fields, such as "any eye color women in New York" or "blue-eyed people of either gender in the USA".
The object is, given a collection of nodes and a collection of patterns, return the collection of patterns that match each node. The patterns have integer IDs for easier reference. Something like this (although this is not really a method in itself--it falls within a larger method):
<pre>
int[][] getMatchingPatterns(java.util.List listNodes, java.util.List listPatterns);
</pre>
Now, it looks like I will have to do listNodes.size() * listPatterns.size() comparisons. So, here's what I've done:
So what you're saying is that I'm probably getting a lot of benefit out of the listPatterns.toArray, because I'm iterating through listPatterns numNodes times. But I'm probably not getting much benefit out of listNodes.toArray because I'm only iterating over listNodes once.
Is there a better way to do this?
--
Susan