title: Converting between jdoubleArray and vector<double> in a Java Native (JNI) method in C++
body: <p>I have a C++ dll that contains methods that I need to use in a Java program. I've been through all the Sun documentation on JNI, and numerous tutorials I found on the web, and I can't seem to find the problem with my code. In fact, a fair portion of the code is cut and pasted from various tutorials and the Sun website.</p>
  
  <p>My specific problem is as follows: My C++ native method receives a jdoubleArray from my Java program, which needs to be mapped to a c++ vector that can then be passed to a method in the C++ dll. The method in the dll returns another vector, that then needs to be mapped to a new jdoubleArray, and returned to the Java program. The problem seems to be that I'm not performing the mapping correctly.</p>
  
  <p>The code I have is as follows (I've removed the call to the dll method for simplicity):</p>
  
  <pre><code>JNIEXPORT jdoubleArray JNICALL Java_jniarraypassing_JNIArrayPassing_passAndReturn(JNIEnv *env, jclass jcls, jdoubleArray arr)
  {
       //First get a pointer to the elements within the jdoubleArray
       jsize len = env-&gt;GetArrayLength(arr);
       jboolean isCopy1;
       jdouble *body = env-&gt;GetDoubleArrayElements(arr, &amp;isCopy1);
  
       //Create the vector&lt;double&gt; and reserve enough memory for mapping
       std::vector&lt;double&gt; newvector;
       newvector.reserve(len);
  
       //Copy the contents of the jdoubleArray to the vector&lt;double&gt;
       for (int i=0; i&lt;len; i++)
       {
          newvector.push_back(body[i]);
       }
  
       //Release the pointer to the jdoubleArray
       if (isCopy1 == JNI_TRUE)
       {
          env-&gt;ReleaseDoubleArrayElements(arr, body, JNI_ABORT);
       }
  
       //Call the dll method here....
  
       jdoubleArray output = env-&gt;NewDoubleArray(newvector.size());
       jboolean isCopy2;
       jdouble* destArrayElems = env-&gt;GetDoubleArrayElements(output, &amp;isCopy2);
       for (int i=0; i&lt;newvector.size(); i++)
       {
          destArrayElems[i] = newvector[i];
       }
       if (isCopy2 == JNI_TRUE) 
       {
              env-&gt;ReleaseDoubleArrayElements(arr, destArrayElems, 0);
       }
  
       return output;
   }
  </code></pre>
  
  <p>When I output the contents of body[i] to the console, I get a series of numbers that look remarkably like Hexadecimal values (e.g. 003DFBE0), but they are all the same, so I don't think they can be memory addresses. </p>
  
  <p>The contents of the returned jdoubleArray consists entirely of default double values (i.e. 0.0), which I suspect is because the values from the original jdoubleArray are invalid?</p>
  
  <p>What I need to find out, is</p>
  
  <p>Any help would be appreciated!</p>