- Creating an Array
- Adding an Element to an Array
- Displaying a Value in an Array
- Looping over an Array
- Manipulating Array Values
- Sorting an Array
- Multidimensional Arrays
- Creating Arrays with More Than Three Dimensions
- Array Aggregate Functions
- Array Utility Functions
- Array Information Functions
3.1. Creating an Array
You want to create a new array.
Use the ArrayNew() function to create a new ColdFusion Array object.
<cfset aPetSounds = ArrayNew(1)>
ColdFusion arrays are different than arrays in many other programming languages in that ColdFusion arrays are sized dynamically. A dynamically sized array is one that allows for expansion as elements are addedthe programmer does not need know the size of the array when it is declared. Instead, ColdFusion sizes the array as the number of elements exceeds the size that ColdFusion initially appropriated.
This dynamic sizing provides flexibility in programming but can also cause a performance hitespecially for large arrays, because ColdFusion essentially has to guess how large the array will eventually be. So if you know the maximum number of elements your array will contain, ColdFusion provides the ArrayResize() function to force a minimum size. ArrayResize() accepts two parameters, the array to resize and the minimum size to set that array. ColdFusion documentation suggests using the ArrayResize() function immediately following an array declaration that will contain more than 500 elements.
Multidimensional arrays are covered later in this chapteruntil then you'll use only single dimensional arrays, created by passing a value of 1 to the ArrayNew() function.
As a best practice, all arrays in this chapter begin with the letter a, as in aArrayName.