Lucrul cu array-uri

Introducere în Julia

James Fulton

Climate informatics researcher

Adăugarea unui element la sfârșitul unui array

# Predefine array
x = [1,2,3,4]

# Add the number 5 to end of array push!(x, 5)
println(x)
[1,2,3,4,5]
Introducere în Julia

Adăugarea unui element la sfârșitul unui array

# Predefine array
x = [1,2,3,4]

# Add the float 5.0 to end of array
push!(x, 5.0)


println(x) println(eltype(x))
[1,2,3,4,5]
Int64
Introducere în Julia

Adăugarea unui element la sfârșitul unui array

# Predefine array
x = [1,2,3,4]

# Add the float 5.2 to end of array
push!(x, 5.2)
ERROR: InexactError: Int64(5.2)
Introducere în Julia

Crearea unui array de tip specificat

# Create float array
x = Float64[1,2,3,4]

println(typeof(x))


# Add the float 5.2 to end of array push!(x, 5.2) println(x)
Vector{Float64}

[1.0, 2.0, 3.0, 4.0, 5.2]
Introducere în Julia

Crearea unui array de tip specificat

# Create empty float array
x = Float64[]

println(typeof(x))
println(x)
Vector{Float64}
Float64[]
Introducere în Julia

Crearea unui array de tip specificat

# Create empty string array
x = String[]

println(typeof(x))
println(x)
Vector{String}
String[]
Introducere în Julia

Adăugarea elementelor la sfârșitul unui array

# Create empty string array
x = String[]

# Add some elements to the array
push!(x, "one") 
push!(x, "two") 
push!(x, "three") 

println(x)
["one", "two", "three"]
# Create empty string array
x = String[]

# Add some elements to the array
append!(x, ["one", "two", "three"])




println(x)
["one", "two", "three"]
Introducere în Julia

Eliminarea ultimului element

x = [1,2,3,4]

# Remove 1 element from end
x = x[1:end-1]

println(x)

[1, 2, 3]
x = [1,2,3,4]

# Remove 1 element from end
last_element = pop!(x)


println(x) println(last_element)
[1, 2, 3]
4
Introducere în Julia

Crearea unui array de lungime definită

# Create integer array with 4 zeros
x = zeros(Int64, 4)


println(x)
[0, 0, 0, 0]
Introducere în Julia

Înlocuirea unui element

# Create integer array with 4 zeros
x = zeros(Int64, 4)

# Replace element in position 3 with value 1
x[3] = 1


println(x)
[0, 0, 1, 0]
Introducere în Julia

Înlocuirea mai multor elemente

# Create integer array with 4 zeros
x = zeros(Int64, 4)

# Replace many elements
x[2:3] = [2,3]


println(x)
[0, 2, 3, 0]
Introducere în Julia

Rezumat

  • Adăugare element - push!(x, 1)
  • Adăugare mai multe elemente - append!(x, [1,2,3])
  • Eliminare ultimul element - pop!(x)
  • Array de tip specificat - Int64[1,2,3], Float64[1,2,3], etc.
  • Array gol de tip specificat - Int64[], Float64[], etc.
  • Array de zerouri - zeros(Int64, n)
  • Înlocuire element - x[index] = value
  • Înlocuire mai multe elemente - x[a:b] = [value1, value2, ...]
Introducere în Julia

Să exersăm!

Introducere în Julia

Preparing Video For Download...