W
W
Weageoo2011-10-11 15:26:28
2D
Weageoo, 2011-10-11 15:26:28

Traversing a two-dimensional array with a single foreach in C#?

Subject:

<font color="black"><font color="#0000ff">using</font> System;<br/>
<font color="#0000ff">using</font> System.Collections.<font color="#2B91AF">Generic</font>;<br/>
<font color="#0000ff">using</font> System.Linq;<br/>
<br/>
<font color="#0000ff">namespace</font> Test<br/>
{<br/>
&nbsp;&nbsp;<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#0000ff">class</font> ExMethods<br/>
&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">public</font> <font color="#0000ff">static</font> <font color="#2B91AF">IEnumerable</font>&lt;Tuple&lt;<font color="#0000ff">int</font>, <font color="#0000ff">int</font>&gt;&gt; Create2DIterator(<font color="#0000ff">this</font> <font color="#0000ff">int</font>[,] arr2D)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">for</font> (<font color="#0000ff">int</font> i = 0, j; i &lt; arr2D.GetLength(0); ++i)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">for</font> (j = 0; j &lt; arr2D.GetLength(1); ++j)<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">yield</font> <font color="#0000ff">return</font> Tuple.Create(i, j);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;<font color="#0000ff">class</font> Program<br/>
&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">static</font> <font color="#0000ff">void</font> Main()<br/>
&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">int</font>[,] arr2D = {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {11, 12, 13},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {21, 22, 23},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {31, 32, 33},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {41, 42, 43},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {51, 52, 53}<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; };<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">foreach</font> (<font color="#0000ff">var</font> t <font color="#0000ff">in</font> arr2D.Create2DIterator())<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">&quot;Row: {0} Col: {1} Val: {2}&quot;</font>, t.Item1, t.Item2, arr2D[t.Item1, t.Item2]);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">&quot;----------&quot;</font>);<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#0000ff">foreach</font> (<font color="#0000ff">var</font> t <font color="#0000ff">in</font> <font color="#0000ff">new</font>[] {<font color="#0000ff">new</font> {Row = 0, Col = 0}, <font color="#0000ff">new</font> {Row = 0, Col = 1}, <font color="#0000ff">new</font> {Row = 0, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 1, Col = 0}, <font color="#0000ff">new</font> {Row = 1, Col = 1}, <font color="#0000ff">new</font> {Row = 1, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 2, Col = 0}, <font color="#0000ff">new</font> {Row = 2, Col = 1}, <font color="#0000ff">new</font> {Row = 2, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 3, Col = 0}, <font color="#0000ff">new</font> {Row = 3, Col = 1}, <font color="#0000ff">new</font> {Row = 3, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 4, Col = 0}, <font color="#0000ff">new</font> {Row = 4, Col = 1}, <font color="#0000ff">new</font> {Row = 4, Col = 2}})<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.WriteLine(<font color="#A31515">&quot;Row: {0} Col: {1} Val: {2}&quot;</font>, t.Row, t.Col, arr2D[t.Row, t.Col]);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font color="#2B91AF">Console</font>.ReadKey();<br/>
&nbsp;&nbsp;&nbsp;&nbsp;}<br/>
&nbsp;&nbsp;}<br/>
}</font><br/>
<br/>
<font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>

Question: how to generate this
<font color="black"><font color="#0000ff">new</font>[] {<font color="#0000ff">new</font> {Row = 0, Col = 0}, <font color="#0000ff">new</font> {Row = 0, Col = 1}, <font color="#0000ff">new</font> {Row = 0, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 1, Col = 0}, <font color="#0000ff">new</font> {Row = 1, Col = 1}, <font color="#0000ff">new</font> {Row = 1, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 2, Col = 0}, <font color="#0000ff">new</font> {Row = 2, Col = 1}, <font color="#0000ff">new</font> {Row = 2, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 3, Col = 0}, <font color="#0000ff">new</font> {Row = 3, Col = 1}, <font color="#0000ff">new</font> {Row = 3, Col = 2},<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <font color="#0000ff">new</font> {Row = 4, Col = 0}, <font color="#0000ff">new</font> {Row = 4, Col = 1}, <font color="#0000ff">new</font> {Row = 4, Col = 2}}</font><br/>
<br/>
<font color="gray">* This source code was highlighted with <a href="http://virtser.net/blog/post/source-code-highlighter.aspx"><font color="gray">Source Code Highlighter</font></a>.</font>

using LINQ, in one line.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
clx, 2011-10-11
@Weageoo

For example like this:

var array = from i in Enumerable.Range(0, 5)
            from j in Enumerable.Range(0, 3)
            select new { Row = i, Col = j };

O
ostapbender, 2011-10-11
@ostapbender

Something likeEnumerable.Range(0, 15).Select(i => new { Row = i / 3, Col = i % 3})

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question