# Defining a array type

**URL:** <https://discourse.julialang.org/t/defining-a-array-type/45472>\
**Category:** General Usage\
**Tags:** array\
**Created:** [August 24, 2020, 8:01pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472 "2020-08-24T20:01:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)\
**Post date:** [August 24, 2020, 8:01pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/1 "2020-08-24T20:01:16Z")

</div>

Newbie question, but how can I define an array type (like Java or C)?

I know is posible to do in local variables like:

```julia
function f(X::Int64)
    Y::Int64 = 0
end

```

How to define an array type?

Thanks!

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 24, 2020, 8:29pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/3 "2020-08-24T20:29:12Z")

</div>

I’m not sure that an Array type is the answer for you.  
If it is, nevermind … 🙂

In general, arrays may have 1,2,…n dimensions. Most of the time, people are working with Vectors, Matricies or other small dimension arrays. To create an array, one easy way is to create it zero-filled:

```julia
julia> element_type = Int
julia> nzeros = 4
julia> myvector = zeros(element_type, nzeros)
4-element Vector{Int64}:
 0
 0
 0
 0

```

To create a matrix, do something similiar

```julia
julia> element_type = Float32
julia> nrows = 3
julia> ncols = 4
julia> mymatrix = zeros(element_type, nrows, ncols)
3×4 Matrix{Float32}:
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0
 0.0 0.0 0.0 0.0

```

Of course you do not need to use the variables.  
`mymatrix = zeros(Float32, 3, 4)` does the same thing.

For 3D arrays, use 3 counts

```julia
julia> my3Darray = zeros(Int, 4, 3, 2)
4×3×2 Array{Int64,3}:
[:, :, 1] =
 0 0 0
 0 0 0
 0 0 0
 0 0 0

[:, :, 2] =
 0 0 0
 0 0 0
 0 0 0
 0 0 0

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [August 24, 2020, 8:29pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/4 "2020-08-24T20:29:27Z")

</div>

I don’t quite understand what you’re asking for. Could you write the Java/C code for which you want the equivalent Julia code?

---

<div class="post-metadata">

**Author:** ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)\
**Post date:** [August 24, 2020, 8:44pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/5 "2020-08-24T20:44:25Z")

</div>

In Java we can write:

```julia
int[] array = {1,2,3};

```

This means that this array only receive int values

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [August 24, 2020, 8:47pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/6 "2020-08-24T20:47:30Z")

</div>

```julia
julia> array = Int[1, 2]
2-element Vector{Int64}:
 1
 2

julia> array = Int[1, 2.0]
2-element Vector{Int64}:
 1
 2

julia> array = Int[1.0, 2.0]
2-element Vector{Int64}:
 1
 2

julia> array[2] = 1.5
ERROR: InexactError: Int64(1.5)

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [August 24, 2020, 8:48pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/7 "2020-08-24T20:48:22Z")

</div>

v = [1, 2]

will automatically make an array of integers (without needing to explicitly specify the type).

---

<div class="post-metadata">

**Author:** ![Renan\_Rabelo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/renan_rabelo/32/8048_2.png) [@Renan\_Rabelo](https://discourse.julialang.org/u/Renan_Rabelo)\
**Post date:** [August 24, 2020, 8:49pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/8 "2020-08-24T20:49:14Z")

</div>

Thanks for the complete answer!  
I didn’t know that I could define the type in the first parameter!

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [August 24, 2020, 8:49pm UTC](https://discourse.julialang.org/t/defining-a-array-type/45472/9 "2020-08-24T20:49:29Z")

</div>

Especially if you’re just starting with the language, don’t use type annotations. You don’t need them.
