With the get
method of an array, we can get an element just by specifying its length. This method allows us to find any element present in an array.
Array.get arr n
arr
: The array whose element we want to get.
n
: The index position of the element or value we want to find or access.
The value returned is an element of the array. If the number n
specified is out of the range of the length of the array then an Invalid_argument
exception is thrown.
(*create some arrays*)let evenNumbers = [| 2; 4; 6; 8; 10;|];;let letters = [|'a'; 'm'; 'e'; 'y'|];;let fruits = [|"banana"; "paw paw"; "apple";|];;(*get their lengths*)print_int(Array.get evenNumbers 3);;print_string("\n");;print_char(Array.get letters 0);; (* first element*)print_string("\n");;print_string(Array.get fruits 1);;print_string("\n");;
Array.get
method and print the results to the console.