Solution:
class SortedVector {Vector vector; // The contained vector.boolean sorted; // Whether the vector is now sorted.SortedVector() { vector = new Vector(); // Construct contained vector. sorted = true; // It is sorted because it is empty. }void AddElement(Object X) { vector.addElement(X); sorted = false; // Adding element generally destroys order }Object ElementAt(int I) { if( !sorted ) // Don't sort unnecessarily { Sort(); // Sort if and only if not already sorted sorted = true; } return vector.elementAt(I); } }