PDA

View Full Version : SWIG and Java - multidimensional arrays



andymadonna
May 5th, 2008, 04:05 AM
Hi everybody,

I have never used SWIG before, but I need to use 3d points generated by a c++ function written with OpenCV in Java. The points would be passed to the java program as an [n][3] array. But I can't figure out through the SWIG documentation how to do this. I have been trying to figure out how to either return the two-dimensional array, or use pointers, or modify a global array.

Basically,

Does anybody know how to use multidimensional arrays in SWIG + Java?


Thank you

Diabolis
July 22nd, 2008, 04:23 PM
What do you mean by usng the array in swing?

An array will look like this:

int ar[] = new int[10];

So a 3D array will be this:

int ar[][][] = new int[10][10][10];

themusicwave
July 22nd, 2008, 06:12 PM
What is SWIG, do you mean SWING? Swing is the Java Graphical User Interface, but I have no clue about SWIG.

If SWIG is what you meant, perhaps providing a link to it would help.

Try giving us some more detail so we can help you.

drubin
July 22nd, 2008, 09:29 PM
Technically java does not support multidimensional arrays. Only arrays of arrays.

This might clear things up a little.



int [][]test =new int[3][3];
test[0]=new int[3];
test[1]=new int[3];
test[2]=new int[3];
//--
test[0][0]=5;


This creates an array of "int array's"(Arrays in Java start indexing at 0).

http://www.faqs.org/docs/javap/c8/s5.html

themusicwave
July 22nd, 2008, 09:40 PM
Technically java does not support multidimensional arrays. Only arrays of arrays.




int [][]test =new int[3][3];
test[0]=new int[3];
test[1]=new int[3];
test[2]=new int[3];
//--
test[0][0]=5;



You don't need to use new int on each cell. Just on the array itself at declaration. This works perfectly fine:



int myArray[][] = new int[3][3];
myArray[0][0] = 0;


Here is a really basic command line app with a multi-dimensional array. Save this as Test.java and run it.




public class Test{

public Test(){
}

public static void main(String args[]){

int myArray[][] = new int[3][5];

for(int i=0; i< myArray.length; i++){
for(int j=0; j< myArray[i].length; j++){
myArray[i][j] = i+j;
}
}

for(int i=0; i< myArray.length; i++){
System.out.println("\r\nNew Row!");
for(int j=0; j< myArray[i].length; j++){
System.out.println(myArray[i][j]);
}
}


}
}


The Output:




New Row!
0
1
2
3
4

New Row!
1
2
3
4
5

New Row!
2
3
4
5
6

drubin
July 22nd, 2008, 10:01 PM
edit: Not sure why it would not accept most of my message before. It said it was to short..
I would change your above code to somthing like this. (Easier to visualize a grid.)



public class Test{

public Test(){
}

public static void main(String args[]){

int myArray[][] = new int[3][5];

for(int i=0; i< myArray.length; i++){
for(int j=0; j< myArray[i].length; j++){
myArray[i][j] = i+j;
}
}

for(int i=0; i< myArray.length; i++){

for(int j=0; j< myArray[i].length; j++){
System.out.print(myArray[i][j]+" "); //adds spacing
}
System.out.println();
}


}
}



You don't need to use new int on each cell. Just on the array itself at declaration. This works perfectly fine:
Yes I know this, but wanted to impress to the OP that each 1d element was in fact just another 1d array.

tinny
July 23rd, 2008, 12:17 AM
I think the OP is asking something quite different from what has been answered so far.

SWIG is used to create a wrapper around code written in C/C++. This wrapper and be used in Java.

Sorry I cant answer the OP's question, ive only read about SWIG I haven't used it.