public static double[] toDoubleArray(DoubleArrayList doubleArrayList) {
    double[] doubleArrayListElements = doubleArrayList.elements();
    return doubleArrayListElements.length == doubleArrayList.size() ? doubleArrayListElements
        : doubleArrayList.toDoubleArray();
  }
@Test
  public void testToDoubleArray() {
    // Test empty list
    DoubleArrayList doubleArrayList = new DoubleArrayList();
    double[] doubleArray = ArrayListUtils.toDoubleArray(doubleArrayList);
    assertEquals(doubleArray.length, 0);

    // Test list with one element
    doubleArrayList.add(1.0);
    doubleArray = ArrayListUtils.toDoubleArray(doubleArrayList);
    assertEquals(doubleArray.length, 1);
    assertEquals(doubleArray[0], 1.0);

    // Test list with multiple elements
    doubleArrayList.add(2.0);
    doubleArrayList.add(3.0);
    doubleArray = ArrayListUtils.toDoubleArray(doubleArrayList);
    assertEquals(doubleArray.length, 3);
    assertEquals(doubleArray[0], 1.0);
    assertEquals(doubleArray[1], 2.0);
    assertEquals(doubleArray[2], 3.0);
  }