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

Scala Array initialization

2 replies
ScalaNoob
Joined: 2009-03-21,
User offline. Last seen 42 years 45 weeks ago.

private Object[][] data = {
{"Mary", "Campione", "Snowboarding", new Integer(5), new
Boolean(false)},
{"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath", "Knitting", new Integer(2), new
Boolean(false)},
{"Sharon", "Zakhour", "Speed reading", new Integer(20), new
Boolean(true)},
{"Philip", "Milne", "Pool", new Integer(10), new
Boolean(false)},
};

I'm new to Scala and I wonder how to convert this kind of two dimensional
Java Array to Scala?
I would really appreciate if someone could help me!

Blair Zajac
Joined: 2009-01-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Array initialization

ScalaNoob wrote:
> private Object[][] data = {
> {"Mary", "Campione", "Snowboarding", new Integer(5), new
> Boolean(false)},
> {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)},
> {"Kathy", "Walrath", "Knitting", new Integer(2), new
> Boolean(false)},
> {"Sharon", "Zakhour", "Speed reading", new Integer(20), new
> Boolean(true)},
> {"Philip", "Milne", "Pool", new Integer(10), new
> Boolean(false)},
> };
>
> I'm new to Scala and I wonder how to convert this kind of two dimensional
> Java Array to Scala?
> I would really appreciate if someone could help me!

You can do

val data = Array(Array("Mary", new java.lang.Integer(5)),
Array("Alison", new java.lang.Integer(7)))

but the inferred type for data is this:

Array[Array[java.lang.Comparable[_ >: java.lang.Integer with java.lang.String <:
java.lang.Comparable[_ >: java.lang.Integer with java.lang.String <:
java.io.Serializable] with java.io.Serializable] with java.io.Serializable]]

or you can do

val data = Array[AnyRef](Array[AnyRef]("Mary", new java.lang.Integer(5)),
Array[AnyRef]("Alison", new java.lang.Integer(7)))

and it's type is:

Array[Array[AnyRef]]

Regards,
Blair

ScalaNoob
Joined: 2009-03-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Scala Array initialization

Thanks for the quick reply! That solves the problem. I have mainly coded in
java and other c-baseed languages, so Scala style and syntax feels sometimes
kinda odd and hard to understand.

Blair Zajac wrote:
>
> ScalaNoob wrote:
>> private Object[][] data = {
>> {"Mary", "Campione", "Snowboarding", new Integer(5), new
>> Boolean(false)},
>> {"Alison", "Huml", "Rowing", new Integer(3), new
>> Boolean(true)},
>> {"Kathy", "Walrath", "Knitting", new Integer(2), new
>> Boolean(false)},
>> {"Sharon", "Zakhour", "Speed reading", new Integer(20), new
>> Boolean(true)},
>> {"Philip", "Milne", "Pool", new Integer(10), new
>> Boolean(false)},
>> };
>>
>> I'm new to Scala and I wonder how to convert this kind of two dimensional
>> Java Array to Scala?
>> I would really appreciate if someone could help me!
>
> You can do
>
> val data = Array(Array("Mary", new java.lang.Integer(5)),
> Array("Alison", new java.lang.Integer(7)))
>
> but the inferred type for data is this:
>
> Array[Array[java.lang.Comparable[_ >: java.lang.Integer with
> java.lang.String <:
> java.lang.Comparable[_ >: java.lang.Integer with java.lang.String <:
> java.io.Serializable] with java.io.Serializable] with
> java.io.Serializable]]
>
>
> or you can do
>
> val data = Array[AnyRef](Array[AnyRef]("Mary", new java.lang.Integer(5)),
> Array[AnyRef]("Alison", new
> java.lang.Integer(7)))
>
> and it's type is:
>
> Array[Array[AnyRef]]
>
> Regards,
> Blair
>

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