This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Accessing Java's Array<int>

8 replies
hrj
Joined: 2008-09-23,
User offline. Last seen 4 years 3 weeks ago.

Hi folks,

How do I access a Java array in Scala?

I could use java.util.Arrays.asList method, except it doesn't work on arrays of primitive types. See:
http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2007-...

Now, I could implement manual boxing in Java, but how do I do it in pure Scala?

thanks,
Harshad

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Accessing Java's Array<int>
scala> val array = new Array[Int](5)
array: Array[Int] = Array(0, 0, 0, 0, 0)

scala> array(1) = 10
                                                                                                                                                                                                                    
scala> array(1)
res1: Int = 10
                                                                                                                                                                                                                    
scala> array
res2: Array[Int] = Array(0, 10, 0, 0, 0)

2009/2/10 Harshad <harshad [dot] rj [at] gmail [dot] com>
Hi folks,

How do I access a Java array<int> in Scala?

I could use java.util.Arrays.asList method, except it doesn't work on arrays of primitive types. See:
http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2007-05/msg01474.html

Now, I could implement manual boxing in Java, but how do I do it in pure Scala?

thanks,
Harshad



hrj
Joined: 2008-09-23,
User offline. Last seen 4 years 3 weeks ago.
Re: Accessing Java's Array<int>

Ricky Clarkson wrote:

> scala> val array = new Array[Int](5)
> array: Array[Int] = Array(0, 0, 0, 0, 0)
>
> scala> array(1) = 10
>
>
> scala> array(1)
> res1: Int = 10
>
>
> scala> array
> res2: Array[Int] = Array(0, 10, 0, 0, 0)

Did I miss something or was it you?

I wanted to use a java array.

The sort you would get from a java.sql.Array.getArray() call

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: Re: Accessing Java's Array<int>


On Tue, Feb 10, 2009 at 1:34 PM, Harshad <harshad [dot] rj [at] gmail [dot] com> wrote:
Ricky Clarkson wrote:

> scala> val array = new Array[Int](5)
> array: Array[Int] = Array(0, 0, 0, 0, 0)
>
> scala> array(1) = 10
>
>
> scala> array(1)
> res1: Int = 10
>
>
> scala> array
> res2: Array[Int] = Array(0, 10, 0, 0, 0)

Did I miss something or was it you?

I wanted to use a java array.

The sort you would get from a java.sql.Array.getArray() call

java.sql.Array.getArray() returns java.lang.Object


--
Viktor Klang
Senior Systems Analyst
Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Re: Accessing Java's Array<int>
A Scala Array maps into a Java Array.  You should be able to pass them interchangeable (with a few rough edges)

On Tue, Feb 10, 2009 at 7:34 AM, Harshad <harshad [dot] rj [at] gmail [dot] com> wrote:
Ricky Clarkson wrote:

> scala> val array = new Array[Int](5)
> array: Array[Int] = Array(0, 0, 0, 0, 0)
>
> scala> array(1) = 10
>
>
> scala> array(1)
> res1: Int = 10
>
>
> scala> array
> res2: Array[Int] = Array(0, 10, 0, 0, 0)

Did I miss something or was it you?

I wanted to use a java array.

The sort you would get from a java.sql.Array.getArray() call



Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Re: Accessing Java's Array<int>
It's you, you missed something.

scala> new java.sql.Array { def getArray() = new Array[Int](5); def free = throw null; def getArray(index: Long, count: Int) = throw null; def getArray(index: Long, count: Int, map: java.util.Map[String, Class[_]]) = throw null; def getArray(map: java.util.Map[String, Class[_]]) = throw null; def getBaseType = throw null; def getBaseTypeName = throw null; def getResultSet = throw null; def getResultSet(index: Long, count: Int) = throw null; def getResultSet(index: Long, count: Int, map: java.util.Map[String, Class[_]]) = throw null; def getResultSet(map: java.util.Map[String, Class[_]]) = throw null }.getArray().asInstanceOf[Array[_]](1)
res9: Any = 0

2009/2/10 Harshad <harshad [dot] rj [at] gmail [dot] com>
Ricky Clarkson wrote:

> scala> val array = new Array[Int](5)
> array: Array[Int] = Array(0, 0, 0, 0, 0)
>
> scala> array(1) = 10
>
>
> scala> array(1)
> res1: Int = 10
>
>
> scala> array
> res2: Array[Int] = Array(0, 10, 0, 0, 0)

Did I miss something or was it you?

I wanted to use a java array.

The sort you would get from a java.sql.Array.getArray() call



hrj
Joined: 2008-09-23,
User offline. Last seen 4 years 3 weeks ago.
Re: Re: Accessing Java's Array<int>

Viktor Klang wrote:

> java.sql.Array.getArray() returns java.lang.Object

Thanks Viktor. I thought the Object instance points to a java array. I am not sure I am using the right terms, but how do I access this
object?

I thought the problem was that was an array of primitive type, because of the limitation of java.util.Arrays.asList method, which I was
trying to use.

Maybe I was barking up the wrong tree.

For reference, here is the javadoc of java.sql.Array.getArray()

Object getArray()
throws SQLException
Retrieves the contents of the SQL ARRAY value designated by this Array object in the form of an array in the Java programming
language. This version of the method getArray uses the type map associated with the connection for customizations of the type
mappings.
Note: When getArray is used to materialize a base type that maps to a primitive data type, then it is implementation-defined whether
the array returned is an array of that primitive data type or an array of Object.
Returns:
an array in the Java programming language that contains the ordered elements of the SQL ARRAY value designated by this Array
object

PS. I could use the java.sql.Array.getResultSet() method as a workaround, but I will go for it only after exploring the getArray()
approach.

Thanks,
Harshad

hrj
Joined: 2008-09-23,
User offline. Last seen 4 years 3 weeks ago.
Re: Re: Accessing Java's Array<int>

Josh Suereth wrote:

> A Scala Array maps into a Java Array. You should be able to pass them
> interchangeable (with a few rough edges)

Then it probably is a bug in the implementation of the JDBC driver I am using (postgresql 8.3) in that it returns a java Object that can't
be cast to scala.Array.

Here is a sample session:
Welcome to Scala version 2.7.2.final (OpenJDK Client VM, Java 1.6.0_0).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import java.sql._

scala> java.sql.DriverManager getConnection("jdbc:postgresql:myDB","user","password")
res0: java.sql.Connection = org.postgresql.jdbc3g.Jdbc3gConnection@fbfb30

scala> res0.prepareStatement("select * from main.test", ResultSet.FETCH_FORWARD,ResultSet.TYPE_FORWARD_ONLY)
res1: java.sql.PreparedStatement = select * from main.test

scala> res1.executeQuery
res2: java.sql.ResultSet = org.postgresql.jdbc3g.Jdbc3gResultSet@cd200f

scala> res2.next
res3: Boolean = true

scala> res2.getArray(1)
res4: java.sql.Array = {1,2,3}

scala> res4.getArray
res5: java.lang.Object = Array(1, 2, 3)

scala> List() ++ res5
:14: error: type mismatch;
found : java.lang.Object
required: Iterable[?]
List() ++ res5
^

scala> res5.asInstanceOf[scala.Array[_]]
java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to scala.runtime.BoxedArray
at .(:14)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAcce...

scala> res5.asInstanceOf[scala.Array[Int]]
java.lang.ClassCastException: [Ljava.lang.Integer; cannot be cast to [I
at .(:14)
at .()
at RequestResult$.(:3)
at RequestResult$.()
at RequestResult$result()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at ...

Thanks,
Harshad

hrj
Joined: 2008-09-23,
User offline. Last seen 4 years 3 weeks ago.
Re: Re: Accessing Java's Array<int>

Harshad wrote:

> Maybe I was barking up the wrong tree.

I indeed was. Apologies for all the spam.

The driver was returning an Array[Integer] while I was assuming it was an Array[Int].

I ended up learning a bit more about JDBC conventions, and about interoperability between Scala and Java arrays, but caused a lot of
spam here.

Sorry :: Thanks :: Cheers :: Nil
Harshad

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland