Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

In the Development with the Force.com Platform book on pg 130, they mention that an Array's length is fixed when you initialize it and that multidimensional arrays are not supported. I just tried using the .add function on an array and it worked so I'm sure what they are talking about, but I couldn't make an array of arrays (which could have just been because I didn't have the right syntax). Ignoring syntax errors, there seems to be some sort of difference and thus maybe an (even if just slight) advantage to using one over the other?

share|improve this question
You can make a list of lists with List<List<String>> twoDimensionalList = new List<List<String>>(); –  Phil Rymek Dec 14 '12 at 16:44

4 Answers 4

up vote 15 down vote accepted

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

share|improve this answer

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

share|improve this answer

All of the above answers are correct. In short you can use the array notation to declare a list. All list methods can be used with it. http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm

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

share|improve this answer

There is one peculiar difference I found and I can not understand why it is there. Here is the code

Integer[] arr = new Integer[1];                       // line 1  
//List < Integer > arr = new List<Integer>();     // line 2  
for(Integer i = 0; i<arr.size() ; i++)

un commenting the first line we get array size as 4 and that one extra element is null at zeroth index. Now commenting the first line and un commenting second line, i.e in list everything works fine and we get expected output. Why this is so? seems like array and list are not exactly the same!

share|improve this answer
The way to delcare an empty array is Integer[] arr = new Integer[0]; or Integer[] arr = new Integer[] {};. You are declaring an array with one (null) entry. Make that change and they will behave the same. –  Keith C Dec 15 '14 at 9:24

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.