# Two dimensional array issue when created with Array.new

**URL:** <https://forum.crystal-lang.org/t/two-dimensional-array-issue-when-created-with-array-new/4565>\
**Category:** Help & Support\
**Created:** [April 10, 2022, 3:04am UTC](https://forum.crystal-lang.org/t/two-dimensional-array-issue-when-created-with-array-new/4565 "2022-04-10T03:04:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/pan/32/1475_2.png) [@Pan](https://forum.crystal-lang.org/u/Pan)\
**Post date:** [April 10, 2022, 3:04am UTC](https://forum.crystal-lang.org/t/two-dimensional-array-issue-when-created-with-array-new/4565/1 "2022-04-10T03:04:41Z")

</div>

The two ways of creating a two dimentional array gets very different results. The first one will fail with a compiling error if commenting out the assigement statement `fail[1] = [1,6,9]` like  
` 2 | fail[1] = [1,6,9] ^ Error: no overload matches 'Array(Array(Int32.class))#[]=' with types Int32, Array(Int32)`.

The second works as expected. Why?

```crystal
fail = Array.new(3) {Array.new(3, Int32)}
#fail[1] = [1,6,9]
p fail #[[Int32, Int32, Int32], [Int32, Int32, Int32], [Int32, Int32, Int32]]

good = Array.new(3) { [] of Int32 }
good[1] = [2,5,8]
p good # [[], [2, 5, 8], []]
```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [April 10, 2022, 3:19am UTC](https://forum.crystal-lang.org/t/two-dimensional-array-issue-when-created-with-array-new/4565/2 "2022-04-10T03:19:02Z")

</div>

The gist of it is, as you can see, they not actually doing the same thing. The equivalent code for the first example would be:

```auto
fail = Array.new(3) { Array(Int32).new 3 }
fail[1] = [1,6,9]

```

In that it creates an array with an initial capacity for three `Int32` values. This is different than `Array.new(3, Int32)` which creates an array of 3 elements, with `Int32.class` as the value for each position. Because you didn’t provide a generic type var of `Int32`, it infers the type of the array as the `Int32.class` of which `[1, 6, 9]` is not compatible with, so it errors. E.g. if you did `Array(Int32).new 3, Int32` the resulting error would be:

```auto
Error: no overload matches 'Array(Int32).new' with types Int32, Int32.class

Overloads are:
 - Array(T).new(size : Int, value : T)
 - Array(T).new(initial_capacity : Int)
 - Array(T).new()
 - Array(T).new(size : Int, & : (Int32 -> T))

```

Which makes it a bit more clear what is going on.
