code: public static String[] appendTextToArray(String[] sarray, final String val,
                                      final int maxEntries) {
      if (sarray == null) {
        if (maxEntries > 0) {
          sarray = new String[1];
          sarray[0] = val;
        }
        return sarray;
      }
  
      if (sarray.length > maxEntries) {
        String[] neb = new String[maxEntries];
        System.arraycopy(sarray, sarray.length - maxEntries,
                         neb, 0, maxEntries);
        sarray = neb;
        sarray[sarray.length - 1] = val;
        neb = null;
        return sarray;
      }
  
      if (sarray.length < maxEntries) {
        int newLen = sarray.length + 1;
        String[] neb = new String[newLen];
        System.arraycopy(sarray, 0,
                         neb, 0, sarray.length);
        sarray = neb;
        sarray[sarray.length - 1] = val;
        neb = null;
  
        return sarray;
      }
  
      if (maxEntries > 1) {
        System.arraycopy(sarray, 1,
                         sarray, 0, sarray.length - 1);
      }
  
      sarray[sarray.length - 1] = val;
      return sarray;
    }
docs: Add a string to a string array of a given maximum length. Truncates
  the string array if required.
  
  New entries go at the end. old get dropped off the front.
  
  @param  sarray     String[] to be updated
  @param  val        new entry
  @param  maxEntries Number of entries we keep.
  @return String[]   Modified sarray