setElement() method now checks indeces are valid before modifying the array.