title: Swift - Dynamic Array of Struct
body: <p>I have a struct:</p>
  
  <pre><code>public struct Deque&lt;T&gt; {
  
      private var array = [T]()
  
      public var isEmpty: Bool {
          return array.isEmpty
      }
  
      public var count: Int {
          return array.count
      }
  
      public mutating func enqueue(_ element: T) { //inserts element at end
          array.append(element)
      }
  
      public mutating func enqueueFront(_ element: T) { //inserts element at beginning
          array.insert(element, at: 0)
      }
  }
  </code></pre>
  
  <p>And I declare the struct like this:</p>
  
  <pre><code>var burst = [Deque&lt;Int&gt;()]
  </code></pre>
  
  <p>And I initialize it like this in a for loop:</p>
  
  <pre><code>for i in 0..&lt;9 {
      for j in 0..&lt;10{
      if processes[i][j] != 0{
          burst[i].enqueue(processes[i][j])
      }
    }
  }
  </code></pre>
  
  <p>I am able to initialize index 0 of my struct successfully, however, whenever i get to index 1, I get an error:</p>
  
  <blockquote>
    <p>Fatal error: Index out of range</p>
  </blockquote>
  
  <p>How do I declare and initialize a dynamic array of structs in swift?</p>