- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Re: Scala Array initialization
Sat, 2009-03-21, 17:28
>>>>> "ScalaNoob" == ScalaNoob writes:
ScalaNoob> private Object[][] data = { {"Mary", "Campione",
ScalaNoob> "Snowboarding", new Integer(5), new Boolean(false)},
ScalaNoob> {"Alison", "Huml", "Rowing", new Integer(3), new
ScalaNoob> Boolean(true)}, {"Kathy", "Walrath", "Knitting", new
ScalaNoob> Integer(2), new Boolean(false)}, {"Sharon", "Zakhour",
ScalaNoob> "Speed reading", new Integer(20), new Boolean(true)},
ScalaNoob> {"Philip", "Milne", "Pool", new Integer(10), new
ScalaNoob> Boolean(false)}, };
ScalaNoob> I'm new to Scala and I wonder how to convert this kind of
ScalaNoob> two dimensional Java Array to Scala? I would really
ScalaNoob> appreciate if someone could help me!
This doesn't look much like a true two-dimensional array; it looks like
a list of records. You already got an answer on how to make an array
of arrays, so I'm going to suggest a different approach.
Do you need it to be mutable? If you don't mind immutable, you might
use a list of tuples:
scala> List(("Mary","Campione","Snowboarding",5,false),
| ("Alison","Huml","Rowing",3,true),
| ("Kathy","Walrath","Knitting",2,false),
| ("Sharon","Zakhour","Speed reading",20,true),
| ("Philip","Milne","Pool",10,false))
res0: List[(java.lang.String, java.lang.String, java.lang.String, Int, Boolean)] = List((Mary,Campione,Snowboarding,5,false), (Alison,Huml,Rowing,3,true), (Kathy,Walrath,Knitting,2,false), (Sharon,Zakhour,Speed reading,20,true), (Philip,Milne,Pool,10,false))
Note that since all the tuples have the same structure, Scala infers the
type List[(String,String,String,Int,Boolean)].
Unless this is throwaway code, though, I would suggest defining a case
class:
case class Person(first:String, last:String, hobby:String, n:Int, flag:Boolean)
scala> case class Person(first:String, last:String, hobby:String, n:Int, flag:Boolean)
defined class Person
scala> List(Person("Mary","Campione","Snowboarding",5,false),
| Person("Alison","Huml","Rowing",3,true))
res1: List[Person] = List(Person(Mary,Campione,Snowboarding,5,false), Person(Alison,Huml,Rowing,3,true))
In Java defining a little class like this would require you to churn out
boilerplate code, so people avoid it and use tricks like Object[][]
instead. But in Scala the class definition is a one-liner, so you might
as well just do it, IMO.
If you need mutability, replace List with Array and/or change the class
definition to:
case class Person(var first:String, var last:String, var hobby:String,
var n:Int, var flag:Boolean)
Sun, 2009-03-22, 06:37
#2
Re: Scala Array initialization
If you are doing anything with this list other than passing
it directly to addRow, or if you have a lot of records, your
code might still be easier to maintain if you use a case
class as Seth suggested, with a conversion to allow calling
DefaultTableMOdel.addRow.
You could define a toArray method in your case class:
case class Person(name:String, n:Int) {
def toArray = Array(name, new java.lang.Integer(n))
}
Then call toArray on each item as you pass it to addRow.
Or you might be able to use an implicit definition to do
that conversion for you automatically. Given the above
case class definition, perhaps you could use something like:
implicit def personToArray(p:Person):Array[Object] = p.toArray
or, if your case class is just the original one-liner, you can put the
conversion into the implicit function definition:
implicit def personToArray(p:Person):Array[Object] =
Array(p.name, new java.lang.Integer)
With the implicit definition, you can just pass a Person to addRow,
and scala will insert a call to the implicit function to do that
conversion for you.
--
Jim
On Sat, Mar 21, 2009 at 10:38:19AM -0700, ScalaNoob wrote:
> Date: Sat, 21 Mar 2009 10:38:19 -0700 (PDT)
> From: ScalaNoob
> To: scala@listes.epfl.ch
> Subject: Re: [scala] Scala Array initialization
>
>
> You are right those are records. This would be better solution, but I have to
> use addRow method of Java DefaultTableModel class. And input data has to be
> either Vector or Object[].
>
>
>
> Seth Tisue wrote:
> >
> >>>>>> "ScalaNoob" == ScalaNoob writes:
> >
> >
> > This doesn't look much like a true two-dimensional array; it looks like
> > a list of records. You already got an answer on how to make an array
> > of arrays, so I'm going to suggest a different approach.
> >
> > Do you need it to be mutable? If you don't mind immutable, you might
> > use a list of tuples:
> >
> > res0: List[(java.lang.String, java.lang.String, java.lang.String, Int,
> > Boolean)] = List((Mary,Campione,Snowboarding,5,false),
> > (Alison,Huml,Rowing,3,true), (Kathy,Walrath,Knitting,2,false),
> > (Sharon,Zakhour,Speed reading,20,true), (Philip,Milne,Pool,10,false))
> >
> > Note that since all the tuples have the same structure, Scala infers the
> > type List[(String,String,String,Int,Boolean)].
> >
> > Unless this is throwaway code, though, I would suggest defining a case
> > class:
> >
> > case class Person(first:String, last:String, hobby:String, n:Int,
> > flag:Boolean)
> >
> > scala> case class Person(first:String, last:String, hobby:String, n:Int,
> > flag:Boolean)
> > defined class Person
> >
> > scala> List(Person("Mary","Campione","Snowboarding",5,false),
> > | Person("Alison","Huml","Rowing",3,true))
> > res1: List[Person] = List(Person(Mary,Campione,Snowboarding,5,false),
> > Person(Alison,Huml,Rowing,3,true))
> >
> > In Java defining a little class like this would require you to churn out
> > boilerplate code, so people avoid it and use tricks like Object[][]
> > instead. But in Scala the class definition is a one-liner, so you might
> > as well just do it, IMO.
> >
> > If you need mutability, replace List with Array and/or change the class
> > definition to:
> >
> > case class Person(var first:String, var last:String, var hobby:String,
> > var n:Int, var flag:Boolean)
> >
> > --
> > Seth Tisue / http://tisue.net
> > lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
> >
> >
>
You are right those are records. This would be better solution, but I have to
use addRow method of Java DefaultTableModel class. And input data has to be
either Vector or Object[].
Seth Tisue wrote:
>
>>>>>> "ScalaNoob" == ScalaNoob writes:
>
>
> This doesn't look much like a true two-dimensional array; it looks like
> a list of records. You already got an answer on how to make an array
> of arrays, so I'm going to suggest a different approach.
>
> Do you need it to be mutable? If you don't mind immutable, you might
> use a list of tuples:
>
> res0: List[(java.lang.String, java.lang.String, java.lang.String, Int,
> Boolean)] = List((Mary,Campione,Snowboarding,5,false),
> (Alison,Huml,Rowing,3,true), (Kathy,Walrath,Knitting,2,false),
> (Sharon,Zakhour,Speed reading,20,true), (Philip,Milne,Pool,10,false))
>
> Note that since all the tuples have the same structure, Scala infers the
> type List[(String,String,String,Int,Boolean)].
>
> Unless this is throwaway code, though, I would suggest defining a case
> class:
>
> case class Person(first:String, last:String, hobby:String, n:Int,
> flag:Boolean)
>
> scala> case class Person(first:String, last:String, hobby:String, n:Int,
> flag:Boolean)
> defined class Person
>
> scala> List(Person("Mary","Campione","Snowboarding",5,false),
> | Person("Alison","Huml","Rowing",3,true))
> res1: List[Person] = List(Person(Mary,Campione,Snowboarding,5,false),
> Person(Alison,Huml,Rowing,3,true))
>
> In Java defining a little class like this would require you to churn out
> boilerplate code, so people avoid it and use tricks like Object[][]
> instead. But in Scala the class definition is a one-liner, so you might
> as well just do it, IMO.
>
> If you need mutability, replace List with Array and/or change the class
> definition to:
>
> case class Person(var first:String, var last:String, var hobby:String,
> var n:Int, var flag:Boolean)
>