title: JNI : create an jobjectArray without knowing the size in advance
body: <p>I would like to return an array of strings from C to Java using JNI.
  I saw that I could use <code>NewObjectArray()</code> this way:</p>
  
  <pre><code>JNIEXPORT jobjectArray JNICALL Java_Array_initStringArray(JNIEnv *env, jclass cls, jint size)
  {
   jclass stringCls = (*env)-&gt;FindClass(env, "Ljava/lang/String;");
   if (stringCls == NULL) return NULL;
  
   jstringArray result = (*env)-&gt;NewObjectArray(env, size, StringCls, NULL);
   if (result == NULL) return NULL; 
   ...
  }
  </code></pre>
  
  <p>But here, I don't know the <code>int size</code> parameter: I don't know the number of strings I'll use to populate my array. So is there a way to create an jobjectArray without knowing in advance the size of the array? </p>
  
  <p>Something like creating an empty array and then adding jobject one by one to that array?</p>
  
  <hr>
  
  <p><strong>EDIT</strong> : solution using an ArrayList as Edwin suggested</p>
  
  <pre><code>jclass arrayClass = (*jenv)-&gt;FindClass(jenv, "java/util/ArrayList");
    if (arrayClass == NULL)   return NULL;
  
  jmethodID mid_init =  (*jenv)-&gt;GetMethodID(jenv, arrayClass, "&lt;init&gt;", "()V");
    if (mid_init == NULL) return NULL;
  
  jobject objArr = (*jenv)-&gt;NewObject(jenv, arrayClass, mid_init));
    if (objArr == NULL) return NULL;
  
  mid_add = (*jenv)-&gt;GetMethodID(jenv, arrayClass, "add", "(Ljava/lang/Object;)Z");
    if (mid_add == NULL) return NULL;
  </code></pre>
  
  <p>Then in a loop I create an <code>jobject obj</code> for each new object I need to add to the arrayList:</p>
  
  <pre><code>jboolean jbool = (*jenv)-&gt;CallBooleanMethod(jenv, objArr, mid_add, obj);
    if (jbool == NULL) return NULL;
  </code></pre>