- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Scala Array initialization
Sat, 2009-03-21, 16:06
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!
Sat, 2009-03-21, 16:37
#2
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
>
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