15.2.2.5 传递字符串数组

来源:互联网 发布:情感读本杂志软件 编辑:程序博客网 时间:2024/06/06 00:45
 public class Sample3{         public native String[] stringMethod(String text);              public static void main(String[] args) throws java.io.UnsupportedEncodingException        {          System.loadLibrary("Sample3");           Sample3 sample = new Sample3();           String[] texts = sample.stringMethod("java编程思想");       for(int i=0;i<texts.length;i++)        {            texts[i]=new String(texts[i].getBytes("ISO8859-1"),"GBK");            System.out.print( texts[i] );       }         System.out.println();      }}/*// Sample3.cpp : 定义 DLL 应用程序的导出函数。//#include "stdafx.h"#include "..\Sample3.h"#include <string.h>#include <stdlib.h>       #define ARRAY_LENGTH 5  JNIEXPORT jobjectArray JNICALL Java_Sample3_stringMethod (JNIEnv *env, jobject obj, jstring string)  {            jclass objClass = env->FindClass( "java/lang/String");         jobjectArray texts= env->NewObjectArray((jsize)ARRAY_LENGTH, objClass, 0);         jstring jstr;           char* sa[] = { "Hello,", "world!", "JNI", "很", "好玩" };         int i=0;         for(;i<ARRAY_LENGTH;i++)          {            jstr = env->NewStringUTF(  sa[i] );            env->SetObjectArrayElement(texts, i, jstr);//必须放入jstring         }         return texts;  }*//*Hello,world!JNI?好?请按任意键继续. . .*/