Open In App

Array Literals in Julia

Last Updated : 11 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Julia is a high performance, dynamic programming language that is easy to use as it has a high-level syntax. It is free for everyone to use. Julia does not give a lot of attention to implementing arrays, so there is a lesser load on the compiler. An array in Julia is represented in a multi-dimensional grid and can contain any type of data. Julia allows us to create arrays not only by creating array variables but also as literals. We will discuss various features of the array literals offered by Julia in this article.

Array Literals

Arrays can be directly constructed in Julia without creating a corresponding variable with the use of square braces with the syntax – [element1, element2, . . . ]. This creates a one-dimensional array where the elements are the ones that are separated by commas in the arguments as shown below:

Julia




# Creating Array of four elements
[11, 22, 33, 44]


Type of an array literal

The type of the array is determined by the type of the elements present in it. If all the elements are of the same type, then the array type will be the same. But if they are different, to get a definitive type for the array we can convert the types of the elements to their promotion types using promote() function.

Julia




# Changing the type of the array
# to the promoted type of the elements
promote(12, 2.3, 4//5)
 
[12, 2.3, 4//5]


 
 

 

If we do not perform this promoting operation an array that can hold any type is produced as shown below:

 

Julia




#  Creating an empty array
[]
#  Creating an array with different types of elements
[11, 3//5]


 
 

Concatenation of Arrays

Vertical concatenation

 

The concatenation of elements in an array is performed by placing a semicolon ( ; )  or a new line between them. These elements will be vertically concatenated. But if a comma is placed between the elements instead of the semicolon, Julia just considers them as elements as shown below:

 

Julia




# No concatenation error occurs,
# but they are treated as elements
# for the array literal
[1:3, 4:7]


As mentioned above we’re separating the elements with a semicolon to perform vertical concatenation:

Julia




# Concatenation using semicolons
[1:3; 4:7]


 
 

 

We could also just represent the array in new lines to perform the concatenation vertically as shown below:

 

Julia




# Concatenation using spacing
[1:2
 3:5
 6]


 
 

Horizontal concatenation 

 

To concatenate the array horizontally we place tabs or spaces between the elements instead of commas. If we do not place the tabs Julia considers the whole argument as one element. We concatenate various types of elements like range elements, array elements, and normal numbers as shown below:

 

Julia




# Horizontal concatenation
[1:2  3:4  5:6]
 
# Elements can also be written in the inner braces
[[1,2]  [3,4]  [5,6]]
 
# Just normal numbers can also be concatenated
[11 22 33]


 
 

Multi-directional concatenation

 

To concatenate both horizontally and vertically we can place newlines and spacing between the elements respectively together without placing commas as shown below:

 

Julia




# Concatenating horizontally and
# vertically in an array literal
[2 4
6 8]


 
 

 

The following example shows the use of zeros() function to fill up some spaces with zeros in a 2D space in the array, vertically concatenate 3 and 6 by placing a semi-colon, a new line to concatenate vertically, and spacing to concatenate 7, 8, and 9 horizontally, which ultimately produces a 3×3 matrix.

 

Julia




# Concatenating four elements horizontally
# and vertically in an array literal
[zeros(Int, 2, 2) [3; 6]
 [7 8]            9]


 
 

 
 

 

Typed array literals 

 

An array of a specific type can be generated with the syntax – T[element1, element2, . . . ], where T is the type you want the array to be. Implementing an array with this syntax creates an array which initializes the type to all the elements in it. The following example shows the initialization of the Int8 type to the elements of the array, but we can also put any other type we desire.

 

Julia




# Creating an array literal
[[1 3]  [2 4]]
 
# Creating a typed array literal of the type Int8
Int8[[1 3]  [2 4]]
 
# Creating a typed array literal of the type Any
Any[[1 3]  [2 4]]




Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads