I'm taking an introductory CS class, and one of the first projects had the following constraints:
You may NOT:
- Make your program part of a package.
- Add additional public methods or variables.
- Use any built in Java Collections Framework classes anywhere in your program (e.g. no
ArrayList,LinkedList,HashSet, etc.).- Use any arrays anywhere in your program.
- Add any additional
importstatements (or use the “fully qualified name” to get around addingimportstatements).
Seeing the constraints for that project made me wonder what other things one could actually do within those constraints. The main limitation that I saw was the "no arrays" clause.
Is it possible to design a data structure subject to these constraints, which emulates the performance characteristics of an array? Specifically, the data structure should represent a sequence of fixed length, supporting operations to "get" or "set" by index, and these operations should take O(1) time, independent of the length of the sequence.
While it would be possible to build graph-like structures, like linked lists and trees, the "get" and "set" operations on these data structures would take O(n) or O(log n) time respectively. The only other thing I can think of is a class with a few thousand private fields and a switch statement to "get" or "set" by index, but this would only work for sequences up to a fixed length.