F # Learning Road (7) collection type

Source: Internet
Author: User
Tags array length arrays

Previous blog, introduced the list type, this article will introduce the array type array, the dictionary type (MAP), as well as the variable group (resizearray), the set type.

One, array type

An array type that semantically represents a set of identical types. This is similar to listing (list), which differs in that the list type data element is immutable and the array type is available. Although the array type is randomly accessed, the performance precedence list is on query access, but the list has better performance on the change collection (adding, removing collection elements). This is similar to most languages.

The syntax for defining an array type is similar to a list, except that the array has two more "|" Meet.

Let arr=[|1;2;3|]
Let jaggedarr=[|
[|1;2|];
[|3;4;5|]
|]

Arr is a one-dimensional array with the type int array. Jaggedarr array is int array array

F # arrays are very similar to C #, there are two categories of arrays, one called rectangular (rectangle) array, the other is called sawtooth (jagged) array

The rectangular array, in numerical sense, is the matrix, and the one-dimensional array is also called a vector or vector. Note jagged arrays have an unequal array length.

In F #, three modules are defined to handle array types. Array,array2,array3, respectively, to deal with one-dimensional, two-dimensional, three-dimensional arrays.

#light

let a=Array2.create 10 20 0
Array2.iteri (fun i j item-> printfn "no %d,%d :%A" i j item) a

System.Console.ReadKey(true) |>ignore

Type A is int [,] and is a two-dimensional array.

Let A=array2.create 10 20 0
A.[5,5]<-8
Array2.iteri (Fun i J item-> printfn "no%d,%d:%A" I j Item) A
Let arr=[|1;3;4|]
Printfn "%A" arr.[0..2]

Let c=[|
[|1;2|];
[|3;4;5|]
|]
Printfn "%d" (c.[1].[ 2])

As you can see from the code above, F # supports range-interval access syntax for one-dimensional arrays in addition to the access methods similar to C #.

Second, variable array type (Resizearray)

Resizearray type, just the alias of the. NET type System.Collections.Generic.List generic type. Resizearray length variable, data element variable, obviously this type is difficult to ensure thread safety, should use F # 's list type preferentially.

let b=new ResizeArray<_>()
b.AddRange(seq{for i in 1..100 ->i})
b.[5]<-8
b.ForEach((fun i->printfn "%d" i))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.