Inspectopedia 2025.2 Help

'Collection.toArray()' call style

Reports Collection.toArray() calls that are not in the preferred style, and suggests applying the preferred style.

There are two styles to convert a collection to an array:

  • A pre-sized array, for example, c.toArray(new String[c.size()])

  • An empty array, for example, c.toArray(new String[0])

In older Java versions, using a pre-sized array was recommended, as the reflection call necessary to create an array of proper size was quite slow.

However, since late updates of OpenJDK 6, this call was intrinsified, making the performance of the empty array version the same, and sometimes even better, compared to the pre-sized version. Also, passing a pre-sized array is dangerous for a concurrent or synchronized collection as a data race is possible between the size and toArray calls. This may result in extra nulls at the end of the array if the collection was concurrently shrunk during the operation.

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

ToArrayCallWithZeroLengthArrayArgument
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Performance

Use the inspection options to select the preferred style.

Inspection options

Here you can find the description of settings available for the 'Collection.toArray()' call style inspection, and the reference of their default values.

Prefer empty array

Default value:

Always

Other available settings:

  • According to language level

  • Never (prefer pre-sized array)

Suppressing Inspection

You can suppress this inspection by placing the following comment marker before the code fragment where you no longer want messages from this inspection to appear:

//noinspection ToArrayCallWithZeroLengthArrayArgument

More detailed instructions as well as other ways and options that you have can be found in the product documentation:

Inspection Details

By default bundled with:

IntelliJ IDEA 2025.2, Qodana for JVM 2025.2,

Last modified: 18 September 2025