Mark
Mark

Reputation: 1663

F# Create an empty Array of a pre-defined size

So I'm trying to create an empty Array that is the length of a table row. I know how to get the length of a row, but I haven't got a clue in how to make an array with a pre-defined length. The program i'm making is dynamic so the length of the array will vary depending on the table I'm accessing.

Does anyone know how?

Upvotes: 10

Views: 7620

Answers (1)

Matt Hogan-Jones
Matt Hogan-Jones

Reputation: 3113

You've said you want an empty array, so take a look at Array.zeroCreate<'T>.

From the documentation:

Creates an array where the entries are initially the default value Unchecked.defaultof<'T>.

Example:

let arrayOfTenZeroes : int array = Array.zeroCreate 10

This page has a lot of useful information on F# arrays - have look through it, it should point you in the right direction.

As Panagiotis Kanavos has pointed out in comments, F# differs from a language like C# for array creation, so I will quote directly from the F# language reference article I've linked to above for clarity:

Several functions create arrays without requiring an existing array. Array.empty creates a new array that does not contain any elements. Array.create creates an array of a specified size and sets all the elements to provided values. Array.init creates an array, given a dimension and a function to generate the elements. Array.zeroCreate creates an array in which all the elements are initialized to the zero value for the array's type.

Upvotes: 16

Related Questions