# Initialising typed array of tuples of ints

**URL:** <https://discourse.julialang.org/t/initialising-typed-array-of-tuples-of-ints/44874>\
**Category:** New to Julia\
**Created:** [August 13, 2020, 1:48pm UTC](https://discourse.julialang.org/t/initialising-typed-array-of-tuples-of-ints/44874 "2020-08-13T13:48:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.julialang.org/u/arcadeperfect)\
**Post date:** [August 13, 2020, 1:48pm UTC](https://discourse.julialang.org/t/initialising-typed-array-of-tuples-of-ints/44874/1 "2020-08-13T13:48:43Z")

</div>

Hi,  
I’m coming from Python so I’m not too hot on static types.

I want an array of tuples of Ints. The number of elements in the array will change depending on the data set used, but the tuples will always be 4 ints. I have this code working:

```julia
citArr = []

iter = 1
println(citArr)

for i in shapes
    global iter
    println(iter)
    x1 = Int32(round(i[1][1]*100000))
    y1 = Int32(round(i[1][2]*100000))
    x2 = Int32(round(i[end][1]*100000))
    y2 = Int32(round(i[end][1]*100000))

    push!(citArr, (x1,y1,x2,y2))

    iter += 1

end

println(citArr)

```

which yields an array of type “any”. I’m struggling to initialize an empty array of type Tuple and then push an arbitrary number of tuples to it.

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 13, 2020, 1:54pm UTC](https://discourse.julialang.org/t/initialising-typed-array-of-tuples-of-ints/44874/2 "2020-08-13T13:54:27Z")

</div>

`citArr = Array{NTuple{4,Int32},1}()`

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [August 13, 2020, 2:13pm UTC](https://discourse.julialang.org/t/initialising-typed-array-of-tuples-of-ints/44874/3 "2020-08-13T14:13:30Z")

</div>

Since it looks like you’re working with tuples as small vector-like objects, you might find [https://github.com/JuliaArrays/StaticArrays.jl](https://github.com/JuliaArrays/StaticArrays.jl) useful. For example, instead of a tuple of ints, you could store an `SVector{4, Int}` which is just as cheap as a tuple but has convenient vector-like methods (like fast matrix-vector multiplication).

You can also use `map` to construct a concretely-typed vector without having to worry about initializing the type at all:

```julia
julia> citArr = map(1:10) do i
         x1 = Int32(1 * i)
         y1 = Int32(2 * i)
         x2 = Int32(3 * i)
         y2 = Int32(4 * i)
         (x1, y1, x2, y2)
       end
10-element Array{NTuple{4,Int32},1}:
 (1, 2, 3, 4)
 (2, 4, 6, 8)
 (3, 6, 9, 12)
 (4, 8, 12, 16)
 (5, 10, 15, 20)
 (6, 12, 18, 24)
 (7, 14, 21, 28)
 (8, 16, 24, 32)
 (9, 18, 27, 36)
 (10, 20, 30, 40)

```

Note how we got the correct element type for free, and we didn’t have to initialize the collection or push anything.

---

<div class="post-metadata">

**Author:** ![arcadeperfect](https://avatars.discourse-cdn.com/v4/letter/a/7ab992/32.png) [@arcadeperfect](https://discourse.julialang.org/u/arcadeperfect)\
**Post date:** [August 13, 2020, 2:50pm UTC](https://discourse.julialang.org/t/initialising-typed-array-of-tuples-of-ints/44874/4 "2020-08-13T14:50:16Z")

</div>

Thanks for the tips. The StaticArrays look like exactly what I’m after.
