title: Convert a java.util.ArrayList of doubles to Matlab array by creating a java double[] array
body: <p>I have a <code>com.mongodb.BasicDBList</code> that I need to convert to a Matlab array. The code below is done in Matlab and shows the type. All the elements in <code>arr</code> are the of the same type.</p>
  
  <pre><code>&gt;&gt; whos arr
    Name      Size            Bytes  Class                      Attributes
  
    arr       1x1                    com.mongodb.BasicDBList              
  
  &gt;&gt; arr.size()
  ans =
  
     304
  &gt;&gt; v = arr.get(0);
  &gt;&gt; whos v
    Name      Size            Bytes  Class     Attributes
  
    v         1x1                 8  double    
  </code></pre>
  
  <p>I have found a few ways of doing this:</p>
  
  <blockquote>
    <p>cell2mat(arr.toArray.cell)</p>
  </blockquote>
  
  <p>Or:</p>
  
  <pre><code>newArr = javaArray('java.lang.Double', arr.size);
  arr.toArray(newArr);
  </code></pre>
  
  <p>This second is an order of magnitude faster.It creates a Double[] array. Unfortunately Matlab doesn't treat this as a double automatically and thus I would need to convert it to a primitive double. </p>
  
  <blockquote>
    <p>double(newArr)</p>
  </blockquote>
  
  <p>Which cancels out any speed gains from doing it this way compared to the first way.</p>
  
  <p>If it would be possible to create a java <code>double[]</code> in Matlab it would solve my problem. I've tried the following, but since <code>double</code> is a primitive type there is no class associated with it and thus gives me an error.</p>
  
  <pre><code>newArr = javaArray('double', arr.size);
  arr.toArray(newArr);
  </code></pre>
  
  <p>Is there a way to create an array of java doubles in matlab. The java code would be similar to <code>double[ ] arr = new double[x];</code>.</p>