Re: Verifying a list is alphabetized
Roedy Green wrote:
On Fri, 02 Dec 2011 05:58:17 -0800, Patricia Shanahan <pats@acm.org>
wrote, quoted or indirectly quoted someone who said :
Generally, if you are scanning a List that is not known to implement
RandomAccess, it is better use Iterator-based methods as much as
possible. They are almost as fast as indexed access for RandomAccess
lists, and much faster for the other List implementations.
I forgot all about that. I had ArrayList in mind as the only
possibility. Will fix.
ISTR there is a way of knowing if a List can be indexed efficiently.
Check for instanceof java.util.RandomAccess: "Marker interface used by
List implementations to indicate that they support fast (generally
constant time) random access. The primary purpose of this interface is
to allow generic algorithms to alter their behavior to provide good
performance when applied to either random or sequential access list."
For the inOrder check, I am not sure there would be enough performance
difference to make it worth checking - the general version should be
almost as fast as the indexed version for indexed lists. The sort of
situation in which I might use would be sort. The copy to an array is
unnecessary for RandomAccess.
Posting code is so often humiliating, but you learn quite a bit in the
process, and end up with higher quality code all over. We need to
teach people to lose their fear of posting code. You notice how
terrified newbies are of posting code. I wonder why...
I know the feeling. We've both been programming long enough to know how
often another programmer will pick up an issue - that is the point of
practices like pair programming and frequent code reviews.
I think newbies take it a bit personally, but sometimes criticism in
this group gets directed too much to the programmer rather than the program.
Patricia