Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Type in Array

Tags:

julia

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?

like image 398
Georgery Avatar asked Apr 12 '20 12:04

Georgery


People also ask

How do you find the type of an 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.

Is typeof array JavaScript?

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).

Can an array be of type string?

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.


1 Answers

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}
like image 108
Bogumił Kamiński Avatar answered Sep 28 '22 10:09

Bogumił Kamiński