获取 Julia 中数组的元素个数 - length() 方法
length()
是 julia 中的内置函数,用于返回指定数组中存在的元素数。
Syntax: length(A::AbstractArray)
Parameters:
- A: Specified array
Returns: It returns the number of elements present in the specified array.
示例 1:
# Julia program to illustrate
# the use of Array length() method
# Finding the number of elements present in
# the specified 1D array A.
A = [5, 10, 15, 20]
println(length(A))
# Finding the number of elements present in
# the specified 2D array B of size 2 * 2
B = [5 10; 15 20]
println(length(B))
# Finding the number of elements present in
# the specified 3D array C of size 2 * 2*2
C = cat([1 2; 3 4], [5 6; 7 8],
[9 10; 11 12], dims = 3)
println(length(C))
输出:
示例 2:
# Julia program to illustrate
# the use of Array length() method
# Finding the number of elements present in
# the specified 1D array A.
A = [1, 2, 3];
println(length(A))
# Finding the number of elements present in
# the specified 2D array B of size 2 * 2
B = [2 4; 6 8; 10 12];
println(length(B))
# Finding the number of elements present in
# the specified 3D array C of size 2 * 2*2
C = cat([1 2; 3 4], [5 6; 7 8],
[9 10; 11 12], [13 14; 15 16], dims = 4);
println(length(C))
输出: