I have this assingment where I have to implement a single linked list class in java, but, it's required to be implemented entirely with recursive functions, even the constructor.
The constructor given in the project provided by the teacher receives an array of n generic elements with the 3-dot notation (T ... elements) and after scratching my head a little bit (I've never done a recursive constructor) I've come up with something that obviously doesn't work:
public SingleLinkedListImpl(T ... elements) {
if(elements.length<=1){
header = new Node<T>(elements[0]);
} else {
addLast(elements[0]);
elements=Arrays.copyOfRange(elements, 1, elements.length-1);
this(elements);
}
}
The IDE im currently working with, says this cant be done because if I refer to the constructor with this, inside the constructor itself it MUST be the first statement in the function.
How do I proceed with this issue?
BTW: I cant use anything from the Collections API