Home > Articles

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

3.1. Creating an Array

You want to create a new array.

Technique

Use the ArrayNew() function to create a new ColdFusion Array object.

<cfset aPetSounds = ArrayNew(1)>

Comments

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 added—the 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 hit—especially 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 chapter—until then you'll use only single dimensional arrays, created by passing a value of 1 to the ArrayNew() function.

NOTE

As a best practice, all arrays in this chapter begin with the letter a, as in aArrayName.

  • + Share This
  • 🔖 Save To Your Account