title: How to Create Object Array with For Loop in Android
body: <p>I'm trying to create an array of objects with a for loop in Android. The array contains a string taken from a database and an image (for the ease of this I have kept the image to the same one throughout).</p>
  
  <p>I started with the following (which does work):</p>
  
  <pre><code>ItemData[] itemsData = {
                  new ItemData(dbString[0], R.mipmap.ic_launcher),
                  new ItemData(dbString[1], R.mipmap.ic_launcher),
                  new ItemData(dbString[2], R.mipmap.ic_launcher),
                  new ItemData(dbString[3], R.mipmap.ic_launcher),
                  new ItemData(dbString[4], R.mipmap.ic_launcher)
          };
  </code></pre>
  
  <p>I want to create the above within a for loop so that when the number of rows in the database changes then the number of objects created will increase without having to amend the code every time.</p>
  
  <p>I have tried several different implementations and the closest I have got is the following (variable <code>b</code> is the number of rows in the database and <code>dbString[i]</code> is the "Name" field in the row):</p>
  
  <pre><code>ItemData[] itemsData = new ItemData[0];
  for(int i = 0;i &lt; b;i++) {
      ItemData[i] = new ItemData[]{
          new  ItemData(dbString[i], R.mipmap.ic_launcher)
      };
  }
  </code></pre>
  
  <p>However this still does not work. The only error being bought up is that there is an expression expected at <code>ItemData[i]</code> on line 3 above.</p>
  
  <p><code>ItemData</code> is being passed to an adapter to then produce a recyclerview of cards.</p>
  
  <p>I am fairly new to programming in general and have researched this issue but am coming up short with an answer that works.</p>