Ranges and arrays

Ranges come in handy when you have to work with an interval of numbers, for example, one up to thousand: 1:1000. The type of this object, typeof(1:1000), is UnitRange{Int64}. By default, the step is 1, but this can also be specified as the second number; 0:5:100 gives all multiples of 5 up to 100. You can iterate over a range, as follows:

# code from file chapter2arrays.jl    
for i in 1:2:9 
    println(i) 
end 

This prints out 1, 3, 5, 7, 9 on consecutive lines.

In the previous section on Strings, we already encountered the array type when discussing the split function:

a = split("A,B,C,D",",") 
typeof(a) #> Array{SubString{String},1} 
show(a) #> SubString{String}["A","B","C","D"] 

Julia's arrays are very efficient, powerful, and flexible. The general type format for an array is Array{Type, n}, with n number of dimensions (we will discuss multidimensional arrays or matrices in Chapter 6, More on Types, Methods, and Modules). As with the complex type, we can see that the Array type is generic, and all the elements have to be of the same type. A one-dimensional array (also called a vector in Julia) can be initialized by separating its values by commas and enclosing them in square brackets, for example, arr = [100, 25, 37] is a 3-element Array{Int64,1}; the type is automatically inferred with this notation. If you want the type to be Any, then define it as follows: arra = Any[100, 25, "ABC"].

The index starts from 1:

julia> arr[0]
ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [0]
julia> arr[1]
100

Notice that we don't have to indicate the number of elements. Julia takes care of that and lets an array grow dynamically when needed.

Arrays can also be constructed by passing a type parameter and a number of elements:

arr2 = Array{Int64}(undef, 5) # is a 5-element Array{Int64,1} 
show(arr2) #> [326438368, 326438432, 326438496, 326438560, 326438624] 

undef makes sure that your array gets populated with random values of the given type.

You can define an array with 0 elements of type Float64 as follows:

arr3 = Float64[] #> 0-element Array{Float64,1} 

To populate this array, use push!; for example, push!(arr3, 1.0) returns 1-element Array{Float64,1}.

Creating an empty array with arr3 = [] is not very useful because the element type is Any. Julia wants to be able to infer the type!

Arrays can also be initialized from a range with the collect function:

arr4 = collect(1:7) #> 7-element Array{Int64,1} 
show(arr4) #> [1, 2, 3, 4, 5, 6, 7] 

Of course, when dealing with large arrays, it is better to indicate the final number of elements from the start for the performance. Suppose you know beforehand that arr2 will need 10^5 elements, but not more. If you use sizehint!(arr2, 10^5), you'll be able to push! at least 10^5 elements without Julia having to reallocate and copy the data already added, leading to a substantial improvement in performance.

Arrays store a sequence of values of the same type (called elements), indexed by integers 1 through the number of elements (as in mathematics, but unlike most other high-level languages such as Python). Like with strings, we can access the individual elements with the bracket notation; for example, with arr being [100, 25, 37], arr[1] returns 100, and arr[end] is 37. Use an invalid index result in an exception, as follows:

arr[6] #> ERROR: BoundsError: attempt to access 3-element Array{Int64,1} at index [6]

You can also set a specific element the other way around:

arr[2] = 5 #> [100, 5, 37] 

The main characteristics of an array are given by the following functions:

  • The element type is given by eltype(arr); in our example, this is Int64
  • The number of elements is given by length(arr), and here this is 3
  • The number of dimensions is given by ndims(arr), and here this is 1
  • The number of elements in dimension n is given by size(arr, n), and here, size(arr, 1) returns 3

A for...in loop over an array is read-only, and you cannot change elements of the array inside it:

da = [1,2,3,4,5] 
for n in da 
    n *= 2 
end 
da #> 5-element Array{Int64,1}: 1 2 3 4 5 

Instead, use an index i, like this:

for i in 1:length(da) 
    da[i] *= 2 
end 
da #> 5-element Array{Int64,1}: 2 4 6 8 10 

It is easy to join the array elements to a string separated by a comma character and a space, for example, with arr4 = [1, 2, 3, 4, 5, 6, 7]:

join(arr4, ", ") #> "1, 2, 3, 4, 5, 6, 7" 

We can also use this range syntax (called a slice as in Python) to obtain subarrays:

arr4[1:3] #>#> 3-element array [1, 2, 3] 
arr4[4:end] #> 3-element array [4, 5, 6, 7] 

Slices can be assigned to, with one value or with another array:

arr = [1,2,3,4,5]
arr[2:4] = [8,9,10] 
println(arr) #> 1 8 9 10 5

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.157