<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SetIntArrayRegion | Student Projects</title>
	<atom:link href="https://studentprojects.in/tag/setintarrayregion/feed/" rel="self" type="application/rss+xml" />
	<link>https://studentprojects.in</link>
	<description>Microcontroller projects, Circuit Diagrams, Project Ideas</description>
	<lastBuildDate>Sat, 10 Dec 2022 05:19:29 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>
	<item>
		<title>JNI Part 5: JNI Arrays</title>
		<link>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/</link>
					<comments>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/#comments</comments>
		
		<dc:creator><![CDATA[Editorial Team]]></dc:creator>
		<pubDate>Mon, 22 Aug 2011 04:48:41 +0000</pubDate>
				<category><![CDATA[JNI Tutorial]]></category>
		<category><![CDATA[jni]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[SetIntArrayRegion]]></category>
		<category><![CDATA[ReleaseIntArrayElements]]></category>
		<category><![CDATA[jintarray]]></category>
		<category><![CDATA[jchararray]]></category>
		<guid isPermaLink="false">http://studentprojects.in/?p=1688</guid>

					<description><![CDATA[<p>Arrays have dimension. An array’s dimension determines the number of indexes needed to access an element. The standard convention for declaring arrays is: String[] s; // one-dimensional array String s[]; // one-dimensional array String[][] s; // two-dimensional array Declaring the size of the array with the following notation is illegal: String[5] s; // illegal declaration</p>
<p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/">JNI Part 5: JNI Arrays</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></description>
										<content:encoded><![CDATA[<ol>
<li>Arrays have dimension. An array’s dimension determines the number of indexes needed to access an element.</li>
<li>The standard convention for declaring arrays is:
<ul>
<li>String[] s; // one-dimensional array</li>
<li>String s[]; // one-dimensional array</li>
<li>String[][] s; // two-dimensional array</li>
</ul>
</li>
<li>Declaring the size of the array with the following notation is illegal:
<ul>
<li>String[5] s; // illegal declaration</li>
</ul>
</li>
<li>An array implicitly extends java.lang.Object. Therefore, an array is an instance of Object class. Hence, arrays in Java are objects.</li>
<li>Declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array.</li>
<li>Since arrays are Objects they can be initialized using the new operator.</li>
<li>When created, arrays are automatically initialized with the default value of their type.
<ul>
<li>String[] s = new String[100];   // default values: null</li>
<li>boolean[] b = new boolean[4]; // default values: false</li>
<li>int[][] i = new int[10][10];        // default values: 0</li>
</ul>
</li>
<li>The JNI treats primitive arrays and object arrays differently.</li>
<li>Primitive arrays contain elements that are of primitive types such as int and boolean.</li>
<li>Object arrays contain elements that are of reference types such as class instances and other arrays.
<ul>
<li>int[] iarr;</li>
<li>float[] farr;</li>
<li>Object[] oarr;</li>
<li>int[][] arr2;</li>
</ul>
</li>
<li>Accessing primitive arrays in a native method requires the use of JNI functions similar to those used for accessing strings.</li>
<li>The JNI Get&lt;Type&gt;ArrayRegion function copies all the elements in the JNI array into a C buffer of an appropriate type. The second argument is the starting index of the elements, and the third argument is the number of elements to be copied. Once the elements are in the C buffer, we can access them in native code.
<ul>
<li>Example: env-&gt;GetCharArrayRegion(arr, 0, arrLength, chArrRWB);</li>
</ul>
</li>
<li>The JNI also supports a corresponding Set&lt;Type&gt;ArrayRegion function that allows native code to modify the array elements. Arrays of all the primitive types (such as boolean, short, and float) are also supported.</li>
<li>The JNI supports a family of Get/Release&lt;Type&gt;ArrayElements functions (for example, Get/ReleaseIntArrayElements) that allow the native code to obtain a direct pointer to the elements of primitive arrays.</li>
<li>The GetArrayLength function returns the number of elements in primitive or object arrays.</li>
</ol>
<p><strong>Example: Accessing intarray</strong></p>
<pre lang="c" line="1" escaped="true">JNIExport jint JNICALL Java_IntArray_sumArray(JNIEnv *env, jobject obj,
	jintArray arr){
		jsize len = *env-&gt;GetArrayLength(env,arr);
		jint *body = *env-&gt;GetIntArrayElements(env,arr,0);
		for (jint i = 0; i &lt; len; ++i){
			sum += body[i];
		}
	*env-&gt;ReleastIntArrayElements(env, arr, body, 0);
}</pre>
<table class="tbl" border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="30%"><strong>JNI Function</strong></td>
<td width="70%"><strong>Description</strong></td>
</tr>
<tr>
<td>Get&lt;Type&gt;ArrayRegion<br />
Set&lt;Type&gt;ArrayRegion</td>
<td>Copies the contents of primitive arrays to or from a pre-allocated C buffer.</td>
</tr>
<tr>
<td>Get&lt;Type&gt;ArrayElements<br />
Release&lt;Type&gt;ArrayElements</td>
<td>Obtains a pointer to the contents of a primitive array. May return a copy of the array.</td>
</tr>
<tr>
<td>GetArrayLength</td>
<td>Returns the number of elements in the array.</td>
</tr>
<tr>
<td>New&lt;Type&gt;Array</td>
<td>Creates an array with the given length.</td>
</tr>
<tr>
<td>GetPrimitiveArrayCritical<br />
ReleasePrimitiveArrayCritical</td>
<td>Obtains or releases a pointer to the contents of a primitive array. These functions allow virtual machines to disable garbage collection while the native code accesses the contents of primitive arrays.</td>
</tr>
</tbody>
</table>
<p><strong>JNI Array Example program</strong></p>
<p>Let’s write a JNI application that passes an unsorted integer array to a native method, which method sorts and sends it back to the application.</p>
<p>The Java code will be(SortArray.java):</p>
<pre lang="java" line="1" escaped="true">class SortArray {
	private native int[] sort(int[] arr);  //native method 

	static   //static initializer code
	{
		System.loadLibrary("SortArray");
	} 

	public static void main(String[] args)
	{
		int iArr[] = {4,5,2,7,1,9}; // Input array
		int oArr[]; //Output array

		SortArray arr = new SortArray();

		System.out.println("Unsorted array: ");
		for(int i = 0; i &lt; iArr.length; i++){
			System.out.println(iArr[i] + " ");
		}

		oArr = arr.sort(iArr);

		System.out.println("Sorted array: ");
		for(int i = 0; i &lt; oArr.length; i++){
			System.out.println(oArr[i] + " ");
		}
	}
}</pre>
<p>Compile SortArray.java (javac SortArray.java) and generate hedder file(javah -jni SortArray).</p>
<p>The generated with javah header file SortArray.h will be:</p>
<pre lang="c" line="1" escaped="true">/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class SortArray */

#ifndef _Included_SortArray
#define _Included_SortArray
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     SortArray
 * Method:    sort
 * Signature: ([I)[I
 */
JNIEXPORT jintArray JNICALL Java_SortArray_sort
  (JNIEnv *, jobject, jintArray);

#ifdef __cplusplus
}
#endif
#endif</pre>
<p>The C++ implementation file SortArray.cpp will be:</p>
<pre lang="cpp" line="1" escaped="true">#include "SortArray.h"
#include "jni.h"

/*
 * Class:     SortArray
 * Method:    sort
 * Signature: ([I)[I
 */
JNIEXPORT jintArray JNICALL Java_SortArray_sort
    (JNIEnv *env, jobject obj, jintArray arr){

    jsize arrLength = env-&gt;GetArrayLength(arr);
    jintArray arrSorted = env-&gt;NewIntArray(arrLength); 

    jint *arrOut = NULL;
    arrOut = env-&gt;GetIntArrayElements(arr, 0);

    for(jsize x = 0; x &lt; arrLength; x++){
        for(jsize y = 0; y &lt; arrLength - 1; y++){
                        if(arrOut[y] &gt; arrOut[y+1]){
				jsize temp = arrOut[y+1];
				arrOut[y+1] = arrOut[y];
				arrOut[y] = temp;
			}
		}
	}

    env-&gt;SetIntArrayRegion(arrSorted, 0, arrLength, arrOut);
    env-&gt;ReleaseIntArrayElements(arr, arrOut, 0);

    return arrSorted;
}</pre>
<p>Run the program (java SortArray) and verify the output.</p><p>The post <a href="https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/">JNI Part 5: JNI Arrays</a> first appeared on <a href="https://studentprojects.in">Student Projects</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://studentprojects.in/software-development/jni/jni-tutorial/jni-part-5-jni-arrays/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
