Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I can't tell from your code what you actually want to do.</p> <p>If you want traverse your array with iterators of iterators, there's already an easy way to do it:</p> <pre><code>val a2d = Array.tabulate(4,4)((i,j)=&gt;4*i+j) a2d.iterator.map(_.iterator) </code></pre> <p>And if you decide you want a single iterator, you can do that too:</p> <pre><code>a2d.iterator.flatMap(_.iterator) </code></pre> <p>If you want to traverse <em>columns</em> even though the array is in row-major order, then you have a little more work to do (which I think is what you were trying to do, but you mixed up your array indices, and maybe some other things):</p> <pre><code>def iterateColumns(aai: Array[Array[Int]]) = new Iterator[Iterator[Int]] { private[this] var j = -1 private[this] val shortest = if (aai.length==0) 0 else aai.map(_.length).min def hasNext = j+1 &lt; shortest def next = { j += 1 new Iterator[Int] { private[this] var i = -1 def hasNext = i+1 &lt; aai.length def next = { i += 1 aai(i)(j) } } } } </code></pre> <p>Now you can</p> <pre><code>scala&gt; for (row &lt;- a2d.iterator.map(_.iterator)) println(row.mkString(" ")) 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 scala&gt; for (col &lt;- iterateColumns(a2d)) println(col.mkString(" ")) 0 4 8 12 1 5 9 13 2 6 10 14 3 7 11 15 </code></pre> <p>(You also ought to be able to do <code>a2d.view.transpose.iterator.map(_.iterator)</code> to avoid making a copy, but unfortunately it doesn't look like this works the way you'd hope in 2.8.1.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload