You are asked to implement a simple generic list type without using any built-in collection classes (e.g., no ArrayList, LinkedList, Vector, etc.). You may use primitive arrays / custom nodes.
Part 1 — Implement a List
Design and implement MyList<T> with at least the following operations:
-
void add(T value)
-
T get(int index)
-
void set(int index, T value)
(or return a new value)
-
T remove(int index)
-
int size()
Requirements:
-
Handle invalid indices (define behavior: throw an exception or return a sentinel).
-
Aim for reasonable time complexity (state your choice of underlying structure: dynamic array or linked list).
Part 2 — Implement an Iterator
Extend MyList<T> so it is iterable.
-
Implement an iterator type with standard iterator methods (e.g.,
hasNext()
and
next()
; optionally
remove()
if you support it).
-
The iterator should traverse the list from index
0
to
size()-1
.
Part 3 — Implement a map function
Add a map-style higher-order function:
-
MyList<R> map(Function<T, R> f)
(or equivalent in your language)
It should:
-
Apply
f
to each element in order
-
Return a
new
MyList<R>
containing the mapped results
-
Not modify the original list
Clarifications
-
Assume single-threaded use.
-
You may assume elements can be
null
unless you choose to forbid it (but document your choice).