0

I am interfacing with a Java library which takes in a double[] as parameter and add elements to the array. I tried creating a Scala mutable array with JavaConversions but it is not able to manage this conversion. Any ideas?

Java:

 public static double libraryFn(double[] numbers) {
    .....
    numbers[0] = 1.0
 } 

Scala:

def caller() {
     // Does not work
     val myNumbers =  new Array[java.lang.Double](1)
    libraryFn(myNumbers)
 }

Thanks for the responses. Looking at the library source more closely, it looks like the library has a bug and was not a scala/java issue at all.

4
  • Can you post the code that isnt working? Commented Oct 25, 2013 at 0:34
  • 1
    Use Array[java.lang.Double] stackoverflow.com/questions/3940699/passing-java-array-to-scala Commented Oct 25, 2013 at 0:35
  • That question is about calling a scala function with Java array. More importantly there is no issue with mutability. What I have is a java function that takes in a double[] and adds a number to the array (out parameter). Commented Oct 25, 2013 at 0:46
  • java.lang.Double is not Java's double. It needs to be Array[Double], in Scala. Commented Oct 25, 2013 at 6:28

1 Answer 1

5

I wonder what is this mutable array you speak of. Just use Array[Double] -- that is the Java double[].

Sign up to request clarification or add additional context in comments.

2 Comments

val myNumbers = Array[Double](10); libraryFn(myNumbers) throws java.lang.ArrayIndexOutOfBoundsException.
@Parag That's an issue with the array size, not with its type. Also, Array[Double](10) creates an array size 1 with 10.0 as its single element. If you want an array of size 10, you should do new Array[Double](10), or use Array.fill or similar methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.