Java Snippet vs. Java Snippet (simple): Array handling

Hi
 

I have an input array with from elements of type double in a collection column called input_vector. I calculate the square of each element and then write a new output array called output_vector.

In a Java Snippet (simple) I use this code and it works:

Double [] output_vector = new Double [$input_vector$.length];
 
for (int i = 0; i < $input_vector$.length; i++){
    output_vector[i] = Math.pow($input_vector$[i],2);
}
return output_vector;

 

But I wonder why I am not able to do a simliar thing  with a Java Snippet? All the needed declarations are there. No errors, but the column output_vector shows only fields with NULL value. I am not able to generate an output array.

Can someone help to transfer this code to a Java Snippet?

 

Frank

 

Hi Frank,

they both work as expected, but you cannot simply copy/paste the code from the Java Snippet (simple) to the Java Snippet node. The two nodes use different conventions, for example when referencing the columns within the input table, so you need to adapt the code when going from one to the other.

Also in the Java Snippet node you need to configure the output properly.

Have a look at the attached sample workflow based on your use case.

Cheers,
Marco.

Thanks. Now the difference between Java Snippet(Simple) and the Java Snippet node is clear to me. As a Java Beginner I always struggle with the declaration of variables.

 

Frank

 

You are welcome Frank, glad my example was useful.

Cheers,
Marco.