The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Please add some code with Mutidimensional arrays

2 replies on 1 page. Most recent reply: Jan 29, 2008 10:04 PM by Eric Willigers

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Javier Diaz Soto

Posts: 29
Nickname: javierbds
Registered: Sep, 2005

Please add some code with Mutidimensional arrays Posted: Jan 22, 2008 5:58 AM
Reply to this message Reply
Advertisement
I found no mention if them and, as they are so different from C-like (and as all functional languages try to force you to use inmutable lists ...)
It makes it difficult, at first, to try things:

var a = new Array[Array[Int]](2,3)
a(0)(1)=4

Is there some sugar for specifying the type?
Is there a simpler syntax?


Javier Diaz Soto

Posts: 29
Nickname: javierbds
Registered: Sep, 2005

Re: Please add some code with Mutidimensional arrays Posted: Jan 22, 2008 6:05 AM
Reply to this message Reply
On the other hand, these are not really multidimensional arrays (I think). Memory seems not to be contiguous and offset calculations cannot be used to position yourself (you seem to need indirection, 1 jump per additional dimension) ... Maybe it is optimized under the hood? If this is not so, how could we use Java multidim arrays in Scala source code?

Eric Willigers

Posts: 8
Nickname: ewilligers
Registered: Dec, 2007

Re: Please add some code with Mutidimensional arrays Posted: Jan 29, 2008 10:04 PM
Reply to this message Reply
Scala doesn't have multidimensional arrays. Instead it has arrays of any element type, so you can create arrays of arrays. There are a couple of old examples in the language spec (search for "matrix"); here's some corrected code for those examples:
https://lampsvn.epfl.ch/trac/scala/attachment/ticket/421/MatrixExamples.scala

There are methods for constructing arrays of arrays of arrays of ...:-
val x = new Array[Array[Array[Int]]](2, 3, 4)
x(0) = new Array[Array[Int]](1, 1)
println( x.map(y => y.map(_.toList)).map(_.toList).toList )

List(List(List(0)), List(List(0, 0, 0, 0), List(0, 0, 0, 0), List(0, 0, 0, 0)))

Flat View: This topic has 2 replies on 1 page
Topic: Possible mistakes in chapters 5 to 8 Previous Topic   Next Topic Topic: Chapter 17, Section 4

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use