because the implementation of the function called toString() is different for both of them.
this is the implementation of the toString() for the ArrayList
/**
* Returns a string representation of this collection. The string
* representation consists of a list of the collection's elements in the
* order they are returned by its iterator, enclosed in square brackets
* ({@code "[]"}). Adjacent elements are separated by the characters
* {@code ", "} (comma and space). Elements are converted to strings as
* by {@link String#valueOf(Object)}.
*
* @return a string representation of this collection
*/
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
notice that when there is no next element, this String "[]" will be returned.
this is the implementation of the toString() for the Arrays
/**
* Returns a string representation of the contents of the specified array.
* If the array contains other arrays as elements, they are converted to
* strings by the {@link Object#toString} method inherited from
* {@code Object}, which describes their <i>identities</i> rather than
* their contents.
*
* <p>The value returned by this method is equal to the value that would
* be returned by {@code Arrays.asList(a).toString()}, unless {@code a}
* is {@code null}, in which case {@code "null"} is returned.
*
* @param a the array whose string representation to return
* @return a string representation of {@code a}
* @see #deepToString(Object[])
* @since 1.5
*/
public static String toString(Object[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(String.valueOf(a[i]));
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
notice that when the array is empty then it's null, then the line b.append(String.valueOf(a[i])); will append this string "null" to "["
you can view the source code in your IDE and debug it for more understanding