1

So I've worked with the ndk/c/c++ a little bit, and I know about the jbooleanArray var type, but my question is how would I go about creating a 2d boolean array and returning it to the Java to be used? Here's an example:

#include "retrieveboolarray.h"
#define  LOG_TAG    "!"
//macros to call the Log functions to allow for debugging. the tag is declared on the line above
#define  LOGD(...)  __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

JNIEXPORT jint JNICALL Java_com_example_ndktesting_generateBoolArrays_retrieveBoolArray(
        JNIEnv * env, jobject obj, jint refNum) {
    //refNum will eventually be used to access certain arrays
    return refNum;
}

so right now it's returning an integer, how would I generate a 2d bool array and return it to the Java?

update: here is some code I've tried previously, still with no luck unfortunately

JNIEXPORT jobjectArray JNICALL Java_com_example_ndktesting_generateBoolArrays_retrieveBoolArray(
        JNIEnv * env, jobject obj, jint refNum) {

    jboolean flag = JNI_TRUE;
    jclass stringClass = (*env)->FindClass(env, "java/lang/Boolean");
    jobjectArray row;
    jobjectArray rows;
    jboolean hello = 1;
    int MAXRESPONSE = 5;
    int MAXTEST = 5;

    jsize i, j;
    for (i = 0; i < 5; i++) {
        row = (*env)->NewObjectArray(env, MAXRESPONSE, stringClass, 0);
        for (j = 0; j < 3; j++) {
            (*env)->SetObjectArrayElement(env, row, j, hello);
        }

        if (flag == JNI_TRUE) {
            flag = JNI_FALSE;
            rows = (*env)->NewObjectArray(env, MAXTEST,
                    (*env)->GetObjectClass(env, row), 0);
        }

        (*env)->SetObjectArrayElement(env, rows, i, row);
    }

    return rows;
}

hopefully someone has an idea on how to fix this so I can make/return a bool array.

codingNewb
  • 470
  • 1
  • 8
  • 23

1 Answers1

4

Essentially, you need to use a jobjectArray that contains jbooleanArray instances. See here for a similar example.

Update:

I wrote the code using C++ and converted to C here, but other than that it is tested and works.

JNIEXPORT jobjectArray JNICALL Java_com_example_ndktesting_generateBoolArrays_retrieveBoolArray(
    JNIEnv* env, jobject obj, jint refNum) {
    jclass arrayClass = (*env)->FindClass(env, "[Z");
    int numColumns = 5, numRows = 5;
    jobjectArray twoDArray = (*env)->NewObjectArray(env, numColumns, arrayClass, 0);
    jbooleanArray column;

    // Initialization of the source array
    jboolean source[numColumns][numRows];
    for (int i = 0; i < numColumns; ++i) {
        for (int j = 0; j < numRows; ++j)
            source[i][j] = true;
    }

    for (int i = 0; i < numColumns; ++i) {
        column = (*env)->NewBooleanArray(env, numRows);
        (*env)->SetBooleanArrayRegion(env, column, 0, numRows, source[i]);
        (*env)->SetObjectArrayElement(env, twoDArray, i, column);
    }
    return twoDArray;
}

In your Java code, you will get an Object[]. You can cast each of its elements to a boolean[].

Community
  • 1
  • 1
Dave
  • 4,282
  • 2
  • 19
  • 24
  • unfortunately I've seen that link before, tried to modify it a bunch yesterday with no luck =( I'll include the end result of what I tried in an update to my question – codingNewb Dec 10 '13 at 11:08
  • I'm on my phone now, but I will update with a working example when I get home. – Dave Dec 10 '13 at 11:24
  • @codingNewb Example is available. You should be able to figure it out from there. – Dave Dec 10 '13 at 14:29