code: public Writer write(Writer writer, int indentFactor, int indent)
              throws JSONException {
          try {
              boolean commanate = false;
              int length = this.length();
              writer.write('[');
  
              if (length == 1) {
                  try {
                      JSONObject.writeValue(writer, this.myArrayList.get(0),
                              indentFactor, indent);
                  } catch (Exception e) {
                      throw new JSONException("Unable to write JSONArray value at index: 0", e);
                  }
              } else if (length != 0) {
                  final int newindent = indent + indentFactor;
  
                  for (int i = 0; i < length; i += 1) {
                      if (commanate) {
                          writer.write(',');
                      }
                      if (indentFactor > 0) {
                          writer.write('\n');
                      }
                      JSONObject.indent(writer, newindent);
                      try {
                          JSONObject.writeValue(writer, this.myArrayList.get(i),
                                  indentFactor, newindent);
                      } catch (Exception e) {
                          throw new JSONException("Unable to write JSONArray value at index: " + i, e);
                      }
                      commanate = true;
                  }
                  if (indentFactor > 0) {
                      writer.write('\n');
                  }
                  JSONObject.indent(writer, indent);
              }
              writer.write(']');
              return writer;
          } catch (IOException e) {
              throw new JSONException(e);
          }
      }
docs: Write the contents of the JSONArray as JSON text to a writer.
  
  <p>If <code>indentFactor > 0</code> and the {@link JSONArray} has only
  one element, then the array will be output on a single line:
  <pre>{@code [1]}</pre>
  
  <p>If an array has 2 or more elements, then it will be output across
  multiple lines: <pre>{@code
  [
  1,
  "value 2",
  3
  ]
  }</pre>
  <p><b>
  Warning: This method assumes that the data structure is acyclical.
  </b>
  
  @param writer
  Writes the serialized JSON
  @param indentFactor
  The number of spaces to add to each level of indentation.
  @param indent
  The indentation of the top level.
  @return The writer.
  @throws JSONException