DEV Community

Cover image for Learning Julia (4): Array
Z. QIU
Z. QIU

Posted on • Edited on

1

Learning Julia (4): Array

As a great tool for numerical and scientific computing, Julia has quite modern design and abundant methods for arrays.

Above all, I need to mention the method names() for showing names exported in a module. It is quite useful for me. Below is an example about obtaining names exported by Core module. (I miss still dir() method in Python).

#  Get an array of the names exported by Module Core
ns = names(Core)  
println(ns)

Enter fullscreen mode Exit fullscreen mode

Alt Text

As seen in the output shown above, there is an AbstractArray type in this Core module. I cite from the official documentation: AbstractArray{T,N} is Supertype for N-dimensional arrays (or array-like types) with elements of type T. Array and other types are subtypes of this.
Base.AbstractVector: AbstractVector{T} == AbstractArray{T,1}.
Base.AbstractMatrix: AbstractMatrix{T} == AbstractArray{T,2}.

Array{T,N} is derived from type AbstractArray{T,N}, which defines N-dimensional dense array with elements of type T.

Undef: Alias for UndefInitializer(), which constructs an instance of the singleton type UndefInitializer, used in array initialization to indicate the array-constructor-caller would like an uninitialized array.
Alt Text

Here are my tests:


# same as in Python
a = [1, 2, 4, 8]   ## Type shall be Array{Int64,1}
println("a: ", a, ", size:", size(a))    # get size
println("a: ", a, ", length:", length(a))  # get length 
### ATTENTION: first element's index is 1, not 0
println("a[1]: ", a[1])           # a[1]  => 1
println("a[3]: ", a[3])           # a[3]  => 4
println("a[end]: ", a[end])       # a[end] => 6


a1 = Array{Int64,1}(undef, 4)
a2 = Array{Int64}(undef, 4)

println(" a and a1 are of same type:", typeof(a) == typeof(a1))
println(" a and a2 are of same type:", typeof(a) == typeof(a2))

# Empty array with datatype
b = Int64[] # similar to C#
println("b:", b, ", size: ", size(b))  

# Array with two elements
c = Int16[12, 34]  # attention, here 12 and 34 inside the brackets are not dimensions, they are elements of the array
println("c:", c, ", size: ", size(c))  



# 2-dimension matrix 
matrix22 = [11 22; 33 44] # => 2x2 matrix of type Int64: [1 2; 3 4]
println("matrix22: ", matrix22, ", size: ", size(matrix22)) 
println("matrix22: ", matrix22, ", length:", length(matrix22))  # get length 

matrix = Array{Int64,2}(undef, 2, 2)

println(" matrix22 and matrix are of same type:", typeof(matrix22) == typeof(matrix))

# Use push! and append! to add elements to an array
# Use pop! to remove the last element of an array
##  method ending with ! means that this method modifies the input argument
e = [1,3,5,7]
push!(e,9)     # => [1,3,5,7,9]
println("e:", e, ", size: ", size(e))  
push!(e,11)     # => [1,3,5,7,9,11]
println("e:", e, ", size: ", size(e))  
pop!(e)     # => [1,3,5,7,9] with 11 been removed
println("e:", e, ", size: ", size(e))  
## append can concatenate an array to another
append!(e,[15,17,19]) # => [1,3,5,7,9,15,17,19]
println("e:", e, ", size: ", size(e))  


println("               ")


#  pushfirst! and popfirst!

f = [1,2,3,4,5,6]
popfirst!(f) # => pops the first element 1, a changed to [2,3,4,5,6]
println("f:", f, ", size: ", size(f))  
pushfirst!(f,5) # => push 5 in the first place of the array: [5,2,3,4,5,6]
println("f:", f, ", size: ", size(f))  

## sort and sort!
prices = [7,4,6,9,3]
s1 = sort(prices) # s1 is a new array sorted in incremental order; prices is still [7,4,6,9,3]
println("s1:", s1, ", prices: ", prices) 
s2 = sort!(prices) # s2 and prices are both sorted in incremental order
println("s2:", s2, ", prices: ", prices)


println("               ")


# using ranging to initialize an array
g = collect(1:5) # => 5-element Int64 Array: [1,2,3,4,5]

# partial reading of an array 
g1 = g[1:3] # => [1, 2, 3]
g2 = g[2:end] # => [2, 3, 4, 5]
println("g1:", g1, ", g2: ", g2) 

# splice! 
h = [3,4,5]
h1 = splice!(h,2) # => 4 ; arr updated to [3,5]
println("h1:", h1, ", h: ", h) 

# check if 3 exists in array h
i = in(3, h) # => true
println("3 is in array h:", i) 

# Exception BoundsError 
try
    h[0] # => ERROR: BoundsError() in getindex at array.jl:270
    h[end+1] # => ERROR: BoundsError() in getindex at array.jl:270
catch e
    println(e)
end
Enter fullscreen mode Exit fullscreen mode

Output of this test:
Alt Text

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →