How can I get the type inside an array?
a = [1,2,3]
I can get the type of a
typeof(a)
Vector{Int64}
but I actually want Int64
. First, I thought a newbie work-around could be
typeof(a[1])
Int64
but this is actually not correct, as can be seen here:
a = [1,2,3, missing]
typeof(a)
Vector{Union{Missing, Int64}}
The type of the vector is Union{Missing, Int64}
, but the type of the first element is
typeof(a[1])
Int64
So, how do I get the type of the vector/array?
Array types may be identified by invoking Class. isArray() . To obtain a Class use one of the methods described in Retrieving Class Objects section of this trail.
The typeof keyword is used to differentiate primitive types in JavaScript. It will return one of nine strings: undefined , object (meaning null), boolean , number , bigint , string , symbol , function , or object (meaning any object, including arrays).
Answer: Yes. Just like arrays can hold other data types like char, int, float, arrays can also hold strings. In this case, the array becomes an array of 'array of characters' as the string can be viewed as a sequence or array of characters.
Use the eltype
function:
julia> a = [1,2,3]
3-element Array{Int64,1}:
1
2
3
julia> eltype(a)
Int64
julia> a = [1,2,3, missing]
e4-element Array{Union{Missing, Int64},1}:
1
2
3
missing
julia> eltype(a)
Union{Missing, Int64}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With