Vector searching .. ohh my.
This is really not working I'm trying to search a vector which contains 30 elements of 'all strings'.
I'm looking for the following string within a vector.
<Color=5>
Except that I don't know what the Color is equal to so I can't use (which would in fact work):
System.out.println ("Found at: " + dataFile.indexOf("<Color=5>"));
and ...
System.out.println ("Found at: " + dataFile.indexOf("<Color"));
does not work.. it returns '-1'
Any ideas on how I'm to return the line where "><Color=" is located?">
> Any ideas on how I'm to return the line where "<Color=" is located?
You mean any line starting with "><Color="? Then you have to iterate the Vector and compare every single element using e.g.
dataFile.get(i).toString().startsWith("><Color=");
Disregard the ">" at the start of the String ...
Another alternative, which is simpler, if efficiency isn't a major issue:
Create a custom Comparator that will match strings the way you want.
Then, call vector.toArray, sort the list and perform a binary search using your new comparator, using the methods Arrays.sort and Arrays.binarySearch.
Saves you from implementing the iteration and search logic, although there is a much higher overhead.
You could also try various tricks on the comparator to make the sorting and searching faster. For example,
Write a custom Comparator where by you classify Strings into two sets: Those that match the string you're looking for, and those that don't. Those that don't are larger than those that do, and all strings in the same group are "equal".
If you sort using this "ordering," then, if there was a string matching your criteria, it would now be in the fist position of your list. And, as a guess (since I don't know the EXACT algorithm that Arrays.sort uses), I would expect that a quick sort on a list of elements with only two effective values would be very fast (possibly completing in only two passes through the list, if the pivot is placed in the right position).
On the other hand, if you're going to have to search for multiple different strings, but the commonality is that all strings that match are matched based on the beginning of the string, you could sort based on the natural ordering of Strings, and then do the binary search with the new comparator.
Lots of options exist. You just need to find the best one for your purposes. Once again, this method isn't as efficient as searching the list yourself, but it's cleaner, in that, it separates the comparison code (in the Comparator) from the iteration code (in the binary search, or in the sorting). Another alternative with the same effect would be to set up a visitor design, again using a customized comparator. This method, however, requires more effort, as the standard java libraries don't provide a visitor architecture (at least, not that I'm aware of).
It's a shame that the collections class don't already provide a "contains" method that accepts a customized comparator. That sort of thing could be handy in any number of situations. It's also a shame that there doesn't seem to be visitor construct to go with the collections framework. Oh, well. Maybe someday.
- Adam