Reading Java multidimensional array in C

Hello friends.
Have searched the forum, but haven's found the answer.
I need to access a Java two dimensional array from C ( boolean[][] a; ).
Here's the code in C:
  jbooleanArray aArray; 
  jboolean **a;   
  fid=(*env)->GetFieldID(env, cls, "a", "[[Z"); 
  if (fid==0) { return; }  aArray=(*env)->GetObjectField(env, jobj, fid); 
  len=(*env)->GetArrayLength(env, aArray); 
  a=(jboolean **) (*env)->GetBooleanArrayElements(env, aArray, 0);Array lengths are correctly read separately. When I debug array in C it fails half-way or so:
  for (i=0;i<arows;i++)   
    for (j=0;j<acols;j++)      
      printf("a[%d][%d]=%u ",i,j,a<i>[j]);Error: An unexpected exception has been detected in native code outside the VM.
Unexpected Signal : EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurred at PC=0x189217B4
Is it the correct way to read multidimensional array? Or could it be the problem with jboolean type?
Kev

Thank you for the idea to test an multidimensional array.
I testet the following code with C++ and changed it afterwords (untestet) into the syntax of C. Hope it runs.
boolean[][] is an array of Objects, this Objects are arrays of boolean!
#include "nativetest_BooleanArrayTest.h"
#include <stdio.h>
// without "if(blabla == 0) return;" etc.
JNIEXPORT void JNICALL Java_nativetest_BooleanArrayTest_test
  (JNIEnv * env, jobject obj)
   jsize i, j;
   jclass clazz = (*env)->GetObjectClass(env, obj);
   jfieldID fid = (*env)->GetFieldID(env, clazz, "b", "[[Z");
   jobject jobj = (*env)->GetObjectField(env, obj, fid);
   jobjectArray jarrRows = (jobjectArray)jobj;
   // Get elements of array
   jsize numberOfRows = (*env)->GetArrayLength(env, jarrRows);
   jboolean** carr = new jboolean*[numberOfRows];
   jbooleanArray* jrows = new jbooleanArray[numberOfRows];
   for (i = 0; i < numberOfRows; ++i) {
      jrows[i] = (*env)->GetObjectArrayElement(env, jarrRows, i);
      carr[i]  = (*env)->GetBooleanArrayElements(env, jrows, 0);
jsize numberOfColumns = (*env)->GetArrayLength(env, jrows[0]);
// test
for (i = 0; i < numberOfRows; ++i) {
for (j = 0; j < numberOfColumns; ++j) {
// printf ...
// release
for (i = 0; i < numberOfRows; ++i) {
(*env)->ReleaseBooleanArrayElements(env, jrows[i], carr[i], 0);
delete[] jrows;
delete[] carr;

Similar Messages

  • How to Read Java Byte Array back into SAP xstring Table?

    Hi Guys,
    I have a situation whereby a user can upload a document in a WebDynpro Java Application.  This needs to be saved.  I am trying to see if I can make the location of this file that is being saved to be on our SAP AS.  I have written a SAP RFC that is imported into the Java Application for use.
    The RFC is expecting a table of type TBL1024 which is basically a table of xstring values.  The Java proxy interprets this SAP table as a byte array.  This is my problem... I cannot seem to "break" up the Byte array into lines of 1024 lengt and send it back to SAP.  All that is happening currently is that the byte array creates a singe line in the TBL1024 that consists of only the first 1024 bytes in the array...  This means that only the first 1024 bytes of my document is being saved - making it corrupt.
    Please advise,
    Kind regards,
    Christiaan

    Loop it...
    File file = new File("java.pdf");
    FileInputStream fis = new FileInputStream(file);
    byte[] buf = new byte[1024];
            try {
                for (int readNum; (readNum = fis.read(buf)) != -1;) {
                    // Here populate the RFC table               
            } catch (IOException ex) {

  • For each loop with multidimensional arrays?

    Hi.I have a problem with for each and multidimensional combination.
    public class TestCards
    static int[][] dizi={{1,2},{3,4}};
    static int t ;
    public static void main(String[]args)
         for (int[] i : dizi)
         System.out.println(i);
    The result of the codes above is like this
    [I@9304b1
    [I@190d11
    In other words the result is now integer.It has some strange chars which i dont know
    Pls help me !

    You're printing an array. Arrays don't override toString.
    - You could iterate over both dimensions and print each value yourself.
    - You could iterate over one dimension and call java.util.Arrays.toString to print a row at a time.*
    - You could call java.util.Arrays.deepToString to print the entire thing in one shot.
    *The second option is closest to what you're doing now.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How to pass a multidimensional array to an Oracle procedure?

    How can I pass a multidimensional array to oracle array?
    Thanx in anticipation,

    Look in to passing user defined type back and forth to Oracle. Any type that oracle supports, you can constract on the java side and send it across. Look into the SQLData class.

  • Question on System.arraycopy method and multidimensional array

    I'm trying to copy from single dimensional to multidimensional array using System.arraycopy method. The following is my problem.
    1) I need to specify the index of the multidimensional array while copying. Can I do that ? If yes , how???
    eg ; int a[] = new int[3];
    int b[] = new int[3][2]; I need to copy from a to b
    I tired the following and I'm getting an error.
    System.arraycopy(a,0,b[][1],0,3);
    How Can I achieve the above?? PLease Help --------------

    Java doesn't have multidimensional arrays. When you see an int[][] it's an array of arrays of ints. The arrays of ints might have different lengths like this one:int[][] arr =
    {{1,2,3,4},
    {1,2,3},
    {1,2},
    {1}
    };Do I need to say that arraycopy as you see it would fail in this case?
    If you know what kind of arrays you'll have you can simply implement your own arraycopy method (but it will not be as effecient as System.arraycopy) with a simple for-loop.

  • Multidimensional Array Sort

    Please help. I am very new to java and trying to build a multidimensional array. I have two variables 1)item_name and 2)item_value. These are values that I obtain by looping through a database result set. I need to build and array that can hold these variables. Once the multidimensional array is built I need to be able to sort it by item value.
    For example I would like to do something like this:
    while (rs.next)
    array.name = item_name
    array.value = item_value
    end
    array.sort(value)
    Thanks for any help!

    Don't use a multidimensional array. Use an array of objects, where the objects are of a class that you define. It might be something as simple as class Item that just has two fields--name and value--and their getters and setters. Or maybe just getters if you want the class to be immutable.
    Have the class implement Comparable and sort on value.
    If you sometimes want to sort on value and sometimes on name, then you'll need to either define two Comparators (one for name, one for value) or make the class Comparable on whichever is the more "natural" ordering and define a Comparator for the other one.
    Either put them in an array and use one of the java.util.Arrays.sort methods to sort them, or put them in a List (java.util.LinkedList or java.util.ArrayList) and use Collections.sort to sort them.

  • Multidimensional Arrays + Me + OOP = Fear

    node.java
    class node {
         int[][] grid = new int[0][0];
         void detect(){
              System.out.println(grid);
    game.java
    class game {
         public static void main(String args[]) {
              node a = new node();
              a.detect();
    The output of this code is:
    http://img237.imageshack.us/img237/6556/outputxy7.png
    I am trying to create an imaginary grid so I can implement A* algorithm into my game which will later be an applet.
    Why did the output of this come out so weird? I do not understand multidimensional arrays and I struggle to understand object oriented programming.
    I have known Java since Nov 2006, but I have always dodge some of this difficult stuff. I am totally self taught and I have been learning through the Internet and a very old Java 2 book. So please speak "dumb" to me.
    Thank you,
    Andrew.

    Oh, grow up. If you get insulted this easily, you'll be inconsolable when you have to get a real job. I'm so sick of these children who want help but also want their genius to remain unquestioned.
    Anyway my point wasn't that you altered the image, but rather that you were careless in what you presented as data. Some of the stuff on that image couldn't possibly have worked with the code you posted. (In particular "java node" would not have even run. It wouldn't produce any output at all other than an error message.)
    Part of debugging is gathering real, useful, accurate information. And if you want help debugging you have to share real, useful, and accurate information. If you gather output and then change your code, you should at the very least make this clear.

  • Working with MultiDimensional arrays

    Hi,
    Just wondering if anyone could help with a problem Im
    having with multi-dimensional arrays in Java:-
    I want to create a multi-dimensional array (e.g. 3 dimensions). I know this can be defined by code such as:-
    int[ ][ ][ ] my_array = new int[10][10][10];
    (would create a 10x10x10)
    My problem is that I want to address the array with indexes that aren't known at compile time, trying to do this causes an error:-
    e.g. my_array[0][0][0] = 1; is ok (i.e. the element at 0,0,0 is set to 1)
    but if the indexes 0,0,0 are replaced by the return value of some function,
    e.g.
    my_index1 = generateIndex(x, y, .... etc);
    my_index2 = generateIndex(a, b, ...etc);
    my_index3 = generateIndex(f,g, .. etc);
    (where generateIndex is some function that returns an integer)
    and the array element set with
    my_array[my_index1][my_index2][my_index3] = 3;
    This generates an error. I know that this problem can be overcome through the use of pointers in C++ but since Java doesn't use pointers, im a bit stuck!
    I have come across a method that overcomes this for a 1D array, using the setInt function of the Array class, e.g.
    Array.setInt(my_1Darray, 0, 1); (would set the first value of my_1Darray to 1);
    (i.e. Array.setInt(array_name, int index, int value);
    But I can't see how this works with multidimensional arrays.
    If anyone could shed any light on this problem, that would be great.
    Thanks,
    Peter

    public class NDArray {
            public static void main(String[] s) {
                    int[][][] _3D = new int[10][10][10];
                    for (int c = 0; c < 1000; c++)
                            _3D[rnd()][rnd()][rnd()]=rnd();
                    for (int i=0; i < 10; i++) {
                      for (int j=0; j < 10; j++) {
                        for (int k=0; k < 10; k++) {
                            System.out.print(_3D[i][j][k]);
                            System.out.print(" ");
                      System.out.println();
                    System.out.println();
            static int rnd() {return (int) (10*Math.random());}
    /* output (example):
    7 0 0 4 9 9 3 0 0 2
    0 1 8 9 6 0 0 0 0 0
    5 3 0 5 0 0 1 0 0 3
    5 6 0 5 0 0 3 0 0 0
    0 0 0 0 4 0 0 5 8 6
    0 9 0 9 0 1 0 2 0 0
    3 3 8 6 0 0 1 0 3 4
    9 6 7 0 0 0 3 6 6 3
    2 0 8 7 0 1 4 0 7 0
    8 0 4 4 3 0 0 5 4 0
    0 0 0 0 3 0 1 0 0 4
    9 0 8 9 1 0 9 0 9 0
    0 8 3 4 1 0 8 0 0 2
    3 0 0 7 3 3 0 5 0 0
    0 0 0 1 4 6 0 0 0 3
    3 5 8 5 8 0 8 2 0 4
    4 0 1 7 0 1 0 4 4 0
    6 0 5 0 0 4 0 8 1 0
    0 0 6 6 2 0 0 4 5 0
    0 6 7 0 4 0 7 5 0 0
    2 4 0 5 0 0 0 2 1 7
    0 6 0 9 0 0 6 1 2 0
    0 5 9 0 1 2 4 0 8 6
    0 0 8 0 0 3 3 8 0 0
    4 7 5 9 0 8 1 0 0 9
    2 0 0 3 0 0 8 3 0 7
    0 6 0 6 0 0 0 0 1 0
    0 9 9 8 4 2 0 0 7 2
    4 0 0 9 0 0 0 1 0 6
    7 0 6 5 2 3 7 8 0 2
    0 0 3 5 0 0 0 0 0 0
    3 0 0 0 0 2 0 0 6 0
    6 0 3 3 6 0 4 0 1 6
    0 0 7 2 0 8 0 0 0 0
    0 0 9 0 6 8 2 1 0 0
    9 0 4 1 3 9 3 2 7 7
    0 3 1 0 3 0 0 9 0 5
    0 0 0 0 6 0 4 8 0 5
    1 8 4 6 7 0 0 4 4 0
    0 4 8 0 0 0 6 0 4 0
    0 9 3 0 0 0 6 0 0 8
    6 8 2 9 6 0 0 7 9 0
    7 1 9 0 0 5 0 2 3 0
    0 0 0 0 9 9 0 7 0 9
    9 8 8 2 9 8 0 5 8 0
    2 3 0 4 0 4 1 0 6 9
    3 3 0 0 0 0 7 6 9 3
    6 2 2 1 0 5 8 3 0 6
    0 6 0 0 0 0 0 0 7 5
    0 6 0 0 0 3 3 0 2 0
    3 6 5 5 8 2 0 9 1 0
    8 0 7 0 9 0 9 0 2 0
    0 2 9 0 0 1 2 4 0 2
    3 1 0 2 0 0 0 0 0 0
    0 5 0 3 8 8 3 0 0 0
    9 0 9 0 5 1 0 9 5 0
    8 0 0 8 8 7 0 3 1 0
    4 0 0 0 1 8 0 9 0 5
    0 0 6 6 0 0 5 2 6 8
    0 4 0 9 0 0 2 0 0 3
    0 5 8 1 7 0 0 4 2 0
    6 5 0 0 2 0 6 8 8 7
    0 0 0 0 3 0 8 4 0 0
    2 3 3 0 0 7 6 8 0 4
    4 1 7 3 8 0 2 3 3 0
    1 5 0 0 4 1 3 7 3 1
    0 0 0 6 0 6 0 0 3 0
    3 7 0 4 5 9 5 5 0 8
    3 8 6 4 0 0 0 1 6 0
    0 0 2 0 2 9 0 0 0 5
    0 5 6 0 5 5 4 0 6 7
    0 2 2 0 9 7 4 2 9 0
    4 0 5 4 8 3 0 0 2 0
    0 0 9 3 3 0 8 8 7 0
    0 7 9 7 0 0 0 7 1 0
    2 0 0 0 5 8 2 0 0 5
    2 4 9 6 6 0 0 0 6 0
    0 6 6 7 0 2 0 0 5 2
    0 9 0 4 8 5 1 0 7 6
    0 0 7 0 4 0 3 8 0 9
    9 4 0 0 0 4 0 0 0 5
    2 0 4 7 7 5 4 0 9 0
    0 0 1 0 5 0 1 0 6 0
    0 6 0 9 0 9 0 4 7 0
    5 9 6 6 2 8 8 4 1 4
    9 7 3 2 7 6 0 2 3 0
    3 1 5 0 8 0 0 0 0 0
    9 0 0 3 0 8 7 0 4 0
    8 6 6 4 0 4 6 4 5 0
    0 0 0 0 0 4 4 0 0 9
    8 8 0 2 0 0 0 1 0 0
    6 2 1 1 9 0 5 1 0 0
    0 9 1 0 6 0 4 0 0 0
    9 4 0 3 0 1 0 7 6 0
    0 9 0 7 8 6 0 5 0 0
    0 8 8 9 0 5 7 0 0 0
    0 4 5 1 6 0 5 2 9 3
    6 0 0 0 0 0 0 9 1 0
    5 9 1 9 2 5 3 0 0 9
    2 9 5 1 7 0 0 0 9 0
    */Seems to work just fine. How is your code different from mine?

  • Problems manipulating multidimensional arrays

    hello, I am new to java programming. I would like to know some techniques in which i can change the data within multidimensional arrays. For example, with a 2d array, (int [row][column] a), the index of the rows are the columns, and each column contains an element. I would like to know how to, for example, switch the order of the indexes of the rows and columns. So for row 1, the columns (the indexes) are 0,1,2,3. after switching the order, it would be 3,2,1,0. Is there any possible way to do this? a reply would be appreciated. Thank you.

    Carefully read the following: given a two dimensional array, we can
    define a simple wrapper:public interface ArrayWrapper {
       public int rowDim();
       public int colDim();
       public int get(int row, int col);
       public void set(int row, int col, int value);
    }... here's a very simply 'real' ArrayWrapper:public class RealArrayWrapper implements ArrayWrapper {
       private int[][] array; // what it's all about
       public RealArrayWrapper(int[][] array) { this.array= array; }
       public int rowDim() { return array.length; }
       public int colDim() { return array[0].length; } // assume a non-ragged array
       public int get(int row, int col) { return array[row][col]; }
       public void set(int row, int col, int value) { array[row][col]= value; }
    }... here's a wrapper that does nothing, given another wrapper:public class NilWrapper implements ArrayWrapper {
       private ArrayWrapper wrap;
       public NilWrapper(ArrayWrapper wrap) { this.wrap= wrap; }
       public int rowDim() { return wrap.rowDim(); }
       public int colDim() { return wrap.colDim(); }
       public int get(int row, int col) { return wrap.get(row, col); }
       public void set(int row, int col, int value) { wrap.set(row, col, value); }
    } ... and here's one that reverses all rows:public class RevRowWrapper extends NilWrapper implements ArrayWrapper {
       public RevRowWrapper(ArrayWrapper wrap) { super(wrap); }
       public int get(int row, int col) { return super.get(rowDim()-row-1, col); }
       public int set(int row, int col, int value) { super(rowDim()-row-1, col, value; }
    }... do you get the picture?
    kind regards,
    Jos

  • Code for how to read an integer array from the command prompt...

    hello,
    Could anyone give me the code for how to read an integer array from the command prompt...its very urgent!..

    If you are using a recent version of Java (5 or later) you can use Scanner:
    http://java.sun.com/javase/6/docs/api/java/util/Scanner.html
    That page has some example code on it, too.

  • Creating or updating a java.sql.array object

    I've created an oracle object table which contains a nested table. I've published these types using JPublisher and can extract an object from the table and read the data both the object and the nested table. (The nested table is typed within the java class as java.sql.array).
    Now I'd like to create an object in java and add it to the database. Populating the object attributes and writing them to the database table is not a problem, but how do I populate java.sql.array and write it to the nested table?
    Werner Reiche

    Array sqlArray = rs.getArray("LINES");
    ResultSet data = sqlArray.getResultSet();
    while (data.next()) {
    System.out.println(data.getString(1));
    .getString(1)); // the line's index #
    System.out.println(data.getString(2));
    .getString(2)); // the line's string value
    } I wonder what this would do
    String[] values = (String[]) sqlArray.getArray();

  • Java.util.Arrays.sort for Vector

    I used the java.util.Arrays.sort to sort an array based on the method below.
                  java.util.Arrays.sort(array, 0, total, new ComparatorX());
               repaint();
                class ComparatorX implements java.util.Comparator
              public int compare( Object p1, Object p2){
                   int x1=((Point)p1).x;
                   int x2=((Point)p2).x;
                   if(x1>x2)
                                return 1;
                   if(x1>x2)
                                return -1;
                   return 0;
         }I've since changed the array to a vector. Is there anyway I can keep the comparator. Or how can I sort the vector based on the above method.

    BTW: Don't know if it's just a typing mistake, but your code contains an error:
    class ComparatorX implements java.util.Comparator     {
       public int compare( Object p1, Object p2) {
          int x1=((Point)p1).x;
          int x2=((Point)p2).x;
          if (x1>x2) {
             return 1;
          if (x1>x2) {  // Should be: if (x2 > x1) ...
             return -1;
          return 0;

  • Read two different arrays and display

    I have a text file, which has 1000 of entries in the way mentioned below:
    Kozos customer
    sabze employee
    I am reading the content of text file and saving as an arrays.
    $global:Content = get-content info.txt
    $global:Name=$Content | foreach {
    $hashTab=@{Name="" ; Info=""}
    $hashTab.Name, $hashTab.Info=$_.Trim() -split '\s+'
    New-Object PSObject -Property $hashTab
    $global:Names=$ServerList | select -ExpandProperty Name
    $global:Infos=$ServerList | select -ExpandProperty Info
    How do I read two arrays, I mean read the values of two arrays one by one and display the result as:
    Names Infos
    Kozos Customer
    sabze employee

    We get that you are unable to do this, but it is still not clear precisely what it is that you want, a two-dimensional array, or a structure such as SeanQuinlan suggested? Also:
    there seems no reason for any of the variables to be global in scope.
    Where is the $serverlist variable defined?
    what relationship is there between the file you mention and the data in the $serverlist variable?
    Your code has three arrays, $name, $names, and $infos; which two of these do you want to combine into a single array? If $names and $infos, would this work for you:
        $namesNinfos = $serverlist | select name,info
    I suggest that you describe your situation without trying to state it in powershell (as that is partly where your problem lies). You could, for example, show sample content of the file and each of the two arrays, and show what this would look like in your
    desired two-dimensional array, perhaps like this:
    file info.txt
    Kozos customer
    sabze employee
    array 1:
    Kozos
    sabze
    array 2:
    customer
    employee
    resultant 2D array
    column 1 2
    row
    1 Kozos customer
    2 sabze employee
    As an aside, it could be that we are getting confused by the term "array". languages such as fortran and basic support multidimensional arrays in which each element is addressed by a set of integer indices, the most common form being row and column, as in
    a spreadsheet.
    In Powershell, arrays are generally one dimensional, with a single numerical index being used to select a single element ($row3 = $array[2]). That element can optionally have multiple values if it is defined as an array or a hash. This is referred to as
    a "jagged array" as each element of the array may not all have the same number of sub-elements as each other.
    Powershell also supports true multidimensional arrays, however, these seem fairly rarely used, I suspect because jagged arrays generally provide more easily used functionality.
    You can get more details on this from
    here and
    here.
    This, then, leads to my final question:
    what type of two-dimensional array are you looking for, and how will your script be processing it?
    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

  • Bindind data to multidimensional Array

    I have a multidimensional Array (3D). I have some fields which I want to bind to some TextInputs:
    model.creditosConsumoModel.literaturaCredito[i][j][k]
    And here's a example of the data binding:
    text="{(model.creditosConsumoModel.literaturaCredito[4][12][0] as Apartado).texto}"
    The problem is: When I do a: model.creditosConsumoModel.literaturaCredito = new Array and I define the Array again with new data everything gets blank (all the text Inputs) and I loss the information.
    Resume: It only works the first time, but not the rest of the times.
    Thanks and regards!!!

    @Pauland
    That's what I was thinking as it was an array of arrays.
    @quebequiano
    Generally, for something like this, I make a wrapper object and set the values on that so I don't have to deal with issues like this.  I'm not sure how architecturally sound it is, but it sounds like you are trying to take small pieces of data and prepare them for your view, in which case a wrapper/adapter would provide you with the benefits of databinding.
    P.S Does your name mean "Quebecian" in spanish ?

  • How to set property "java.util.Arrays.useLegacyMergeSort" in jnlp-file?

    Hi,
    I have a problem in a web start application with java 7 and the new sort behavior (exception: comparison method violates its general contract).
    So I like to go back to previous behavior and tried to put the new java 7 system property "java.util.Arrays.useLegacyMergeSort" in the generated jnlp-File:
    <property name="java.util.Arrays.useLegacyMergeSort" value="true"/>.
    Now I can read the property value in java code with System.getProperty("java.util.Arrays.useLegacyMergeSort"), and guess the property was successful set, but the exception still appears?!
    The system property works with command line "javaws -J-Djava.util.Arrays.useLegacyMergeSort=true file.jnlp", but not via doubleclick on the jnlp-file or url and browser.
    Any ideas?
    Cheers,
    Dan

    Only "trusted" set of properties can be set in the JNLP file by unsigned applications.
    List of properties is revised from time to time but it usually takes time for new properties to be added to it (if there is strong demand for it as every property should undergo security audit).
    You can specify arbitrary property if you sign your application and JNLP file.

Maybe you are looking for