code: public static <T> void fill(final List<? super T> list, final int fromIndex, final int toIndex, final T val) {
          checkFromToIndex(fromIndex, toIndex, Integer.MAX_VALUE);
  
          int size = list.size();
  
          if (size < toIndex) {
              if (fromIndex < size) {
                  for (int i = fromIndex; i < size; i++) {
                      list.set(i, val);
                  }
              } else {
                  for (int i = size; i < fromIndex; i++) {
                      list.add(null);
                  }
              }
  
              for (int i = 0, len = toIndex - list.size(); i < len; i++) {
                  list.add(val);
              }
          } else {
              if (toIndex - fromIndex < FILL_THRESHOLD || list instanceof RandomAccess) {
                  for (int i = fromIndex; i < toIndex; i++) {
                      list.set(i, val);
                  }
              } else {
                  final ListIterator<? super T> itr = list.listIterator(fromIndex);
  
                  for (int i = fromIndex; i < toIndex; i++) {
                      itr.next();
  
                      itr.set(val);
                  }
              }
          }
      }
docs: The specified value will be added/inserted into the specified List.
  The List will be extended automatically if the size of the List is less than the specified toIndex.
  @param list
  @param fromIndex
  @param toIndex
  @param val