Is there a difference between an Array and a List in Apex?

There really aren't arrays in Apex, but you can use the array notation to declare lists. From the documentation on lists:

When using one-dimensional lists of primitives or sObjects, you can also use more traditional array notation to declare and reference list elements. For example, you can declare a one-dimensional list of primitives or sObjects by following the data or sObject type name with the [] characters

To answer the question about fixed arrays (lists), it all depends on how you try to put elements in them:

Even though the size of the previous String array is defined as one element (the number between the brackets in new String1), lists are elastic and can grow as needed provided that you use the List add method to add new elements. For example, you can add two or more elements to the colors list. But if you’re using square brackets to add an element to a list, the list behaves like an array and isn’t elastic, that is, you won’t be allowed to add more elements than the declared array size.


In Salesforce basically an array is equivalent to a list only. The array notification declares a list only


In short you can use the array notation to declare a list. All list methods can be used with it. More

The main difference is that Lists can be multi dimensional. i.e, list of lists.

Tags:

Apex