How to Create Array in Julia?

How to Create Array in Julia?

  • Julia
  • 2 mins read

In Julia, an array is a data structure that contains a collection of elements of the same data type. It is similar to a vector in other programming languages, but can have any number of dimensions, allowing it to represent data in a multidimensional format. To create an array in Julia, you can use the Array function. Here's an example:

Create Array in Julia Examples

In the example below, the Array function is used to create an array of type Int64 with elements 1 through 10. The resulting array is then printed to the console.

julia> a = Array{Int64}(1:10)
julia> a
10-element Array{Int64,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

You can also use the [] notation to create an array:

In the example below, the [] notation is used to create an array with the elements 1 through 5. The resulting array is then printed to the console.

julia> b = [1, 2, 3, 4, 5]
julia> b
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

You can also specify the type of the elements in the array when using the [] notation by placing the type in square brackets before the array elements:

In the example below, the [] notation is used to create an array with the elements 1 through 5, and the type of the elements is explicitly specified as Int64. The resulting array is then printed to the console.

julia> c = [Int64(1), Int64(2), Int64(3), Int64(4), Int64(5)]
julia> c
5-element Array{Int64,1}:
 1
 2
 3
 4
 5

Related:

  1. Check if a File Exists in Julia
  2. Check if a Key is in Dictionary using Julia
  3. Check if a Variable Exists in Julia
  4. Check if an Element is in Array in Julia