Yes, create a sub class of that data structure and override add
/ put
method of that data structure, check before adding whether length of array is 3 or not.
Below is the example:
import java.util.Vector;
public class SubClassArrayList<E> extends Vector<E> {
public boolean add(E e , int length) {
if (e instanceof Object[] && ((Object[]) e).length == length) {
return super.add(e);
}
else return false;
}
}
Above will also work for ArrayList