This post is about how to create array literals in Julia. When we write out array values, we create array literals. Julia can readily interpret these array literals. There are other ways to create arrays in Julia, but we will stick with array literals on this post.
Create Empty Literal Arrays
An empty array has a type of Array{Any, 1}, which means it is a one-dimensional array that can have values of any type. Yes, they can store values of different types.
1 2 3 | my_empty_array = [] type_of_array = typeof(my_empty_array) println("Array is $type_of_array with content: $my_empty_array") |
We can push any value to the array, as follows.
1 2 3 | push!(my_empty_array, 2, 5.5, "This is an array element") type_of_array = typeof(my_empty_array) println("Array is $type_of_array with content: $my_empty_array") |
We can also create an empty array of a specific data type. As a result, the array can also accept values of that type.
1 2 | my_empty_int64_array = Int64[] push!(my_empty_int64_array, 2, 5.5, "This is an array element") |
When we run the codes to add values of various types to the array, we get the following error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | C:\Users\karldev\AppData\Local\Programs\Julia\Julia-1.4.0\bin\julia.exe --check-bounds=no --history-file=no --inline=no --color=yes --math-mode=ieee --handle-signals=no --startup-file=yes --optimize=3 --compile=yes --depwarn=yes --code-coverage=none --track-allocation=none C:/Users/karldev/Desktop/dev/blogs/julia/oo/src/ArrayDemo.jl ERROR: LoadError: InexactError: Int64(5.5) Stacktrace: [1] Int64 at .\float.jl:710 [inlined] [2] convert(::Type{Int64}, ::Float64) at .\number.jl:7 [3] setindex!(::Array{Int64,1}, ::Float64, ::Int64) at .\array.jl:825 [4] _append!(::Array{Int64,1}, ::Base.HasLength, ::Tuple{Int64,Float64,String}) at .\array.jl:967 [5] append!(::Array{Int64,1}, ::Tuple{Int64,Float64,String}) at .\array.jl:959 [6] push!(::Array{Int64,1}, ::Int64, ::Float64, ::String) at .\array.jl:960 [7] top-level scope at C:\Users\karldev\Desktop\dev\blogs\julia\array\src\ArrayDemo.jl:2 [8] include(::Module, ::String) at .\Base.jl:377 [9] exec_options(::Base.JLOptions) at .\client.jl:288 [10] _start() at .\client.jl:484 in expression starting at C:\Users\karldev\Desktop\dev\blogs\julia\array\src\ArrayDemo.jl:2 |
Create Two-dimensional Arrays
We can also create two-dimensional array literals. Consider the following codes. Note the semi-colon and spaces between the values.
1 2 3 4 5 6 7 | my_2d_array = ["Kevin" "Kelvin"; "Marc" "Spencer"] println(my_2d_array[1, 1]) # Kevin println(my_2d_array[1, 2]) # Kelvin println(my_2d_array[2, 1]) # Marc println(my_2d_array[2, 2]) # Spencer println(typeof(my_2d_array)) # Array{String,2} |
Create Arrays of Arrays Literals
Arrays of arrays are different from multi-dimensional arrays. For instance, consider the following codes. They show how to create an array of an array literal in Julia.
1 2 3 4 5 6 7 | my_aoa_array = [["Kevin", "Kelvin"], ["Marc" ,"Spencer"]] println(my_aoa_array[1][1]) # Kevin println(my_aoa_array[1][2]) # Kelvin println(my_aoa_array[2][1]) # Marc println(my_aoa_array[2][2]) # Spencer println(typeof(my_aoa_array)) # Array{Array{String,1},1} |
Notice how we use the square brackets now. We are handling more square brackets both in the array definition and in accessing each value in the array. Each pair represents an array. We can also break down the array into individual collections.
1 2 3 4 5 6 7 8 9 10 | a1 = String["Kevin", "Kelvin"] a2 = String["Marc" ,"Spencer"] my_aoa_array = [a1, a2] println(my_aoa_array[1][1]) # Kevin println(my_aoa_array[1][2]) # Kelvin println(my_aoa_array[2][1]) # Marc println(my_aoa_array[2][2]) # Spencer println(typeof(my_aoa_array)) # Array{Array{String,1},1} |
When we use Array{Array{String,1}, 1} to specify the type of the array explicitly, we get an error. Consider the following codes.
1 2 3 4 | a1 = String["Kevin", "Kelvin"] a2 = String["Marc" ,"Spencer"] my_aoa_array = Array{Array{String,1},1}[a1, a2] |
These codes result in an error.
1 2 3 4 5 | ERROR: LoadError: MethodError: Cannot `convert` an object of type String to an object of type Array{String,1} Closest candidates are: convert(::Type{Array{String,1}}, !Matched::LibGit2.StrArrayStruct) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\LibGit2\src\strarray.jl:14 convert(::Type{T}, !Matched::AbstractArray) where T<:Array at array.jl:533 convert(::Type{T}, !Matched::T) where T<:AbstractArray at abstractarray.jl:14 |
To fix the error, we use the following array type.
1 | my_aoa_array = Array{Any,1}[a1, a2] |
Create Array Literals With Default Values
We can also create arrays with default values using comprehensions. Comprehensions do not create array literals, but they provide a convenient means to create arrays with default values. Consider the following codes.
1 2 3 4 5 | with_comprehension = [0 for n in 1:3, n in 1:3] println(with_comprehension) with_out_comprehension = [0 0 0; 0 0 0; 0 0 0] println(with_out_comprehension) |
We tested the codes using Julia 1.4.0.