Tolong pilih kategori sesuai, jenis posting (pertanyaan atau bukan) dan sertakan tag/topik yang sesuai misal komputer, php, mysql, dll.
Promosi atau posting tidak pada tempatnya akan kami hapus.
Mencantumkan kode program di posting Anda, tolong ikuti aturan yang sesuai, baca http://diskusiweb.com/discussion/39204/aturan-cara-menyisipkan-kode-program-di-diskusiweb

Baca cara posting gambar/image di post Anda: http://www.diskusiweb.com/discussion/47345/cara-menyisipkan-menyertakan-image-pada-posting/p1

Cari Max dalam 2D Array dengan LINQ

edited February 2011 in Share
Mencari Max dalam 2 dimensi Array ada banyak cara.
Yang umum dengan menggunakan loop dan compare.

Kali ini kita coba menggunakan LINQ dan sebagai pembantunya kita menggunakan extension fitur.
note: code ini hanya di coba dalam .net 4.0<div class='geshi_syntax'><table><tr><td><div class='code'><pre class="csharp"><span class="co1">// C#</span>
var matrixData <span class="sy0">=</span> <span class="kw3">new</span><span class="br0">[</span>,<span class="br0">]</span> <span class="br0">{</span>
<span class="br0">{</span><span class="nu0">2.22</span>, <span class="nu0">1.98</span>, <span class="nu0">3.96</span>, <span class="nu0">4.98</span>, <span class="nu0">2.29</span><span class="br0">}</span>,
<span class="br0">{</span><span class="nu0">0.62</span>, <span class="nu0">1.22</span>, <span class="nu0">3.22</span>, <span class="nu0">4.92</span>, <span class="nu0">3.77</span><span class="br0">}</span>,
<span class="br0">{</span><span class="nu0">2.83</span>, <span class="nu0">0.97</span>, <span class="nu0">3.98</span>, <span class="nu0">4.92</span>, <span class="nu0">5.88</span><span class="br0">}</span>
<span class="br0">}</span><span class="sy0">;</span>
 
matrixData<span class="sy0">.</span><span class="me1">Print</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
matrixData<span class="sy0">.</span><span class="me1">Cast</span><span class="sy0"><</span><span class="kw4">Double</span><span class="sy0">></span><span class="br0">(</span><span class="br0">)</span><span class="sy0">.</span><span class="me1">Max</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">.</span><span class="me1">Print</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
matrixData<span class="sy0">.</span><span class="me1">MaxPerColoums</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">.</span><span class="me1">Print</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
matrixData<span class="sy0">.</span><span class="me1">MaxPerRows</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">.</span><span class="me1">Print</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
 
 
<span class="co1">// Extension</span>
 
<span class="kw1">static</span> <span class="kw4">class</span> Ext
<span class="br0">{</span>
<span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">void</span> Print<span class="br0">(</span><span class="kw1">this</span> <span class="kw4">Double</span> i<span class="br0">)</span>
<span class="br0">{</span>
Console<span class="sy0">.</span><span class="me1">WriteLine</span><span class="br0">(</span><span class="st0">"Print: Double"</span><span class="br0">)</span><span class="sy0">;</span>
Console<span class="sy0">.</span><span class="me1">WriteLine</span><span class="br0">(</span>i<span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
 
<span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">void</span> Print<span class="br0">(</span><span class="kw1">this</span> IList<span class="sy0"><</span><span class="kw4">Double</span><span class="sy0">></span> arr<span class="br0">)</span>
<span class="br0">{</span>
Console<span class="sy0">.</span><span class="me1">WriteLine</span><span class="br0">(</span><span class="st0">"Print: Double Array"</span><span class="br0">)</span><span class="sy0">;</span>
 
<span class="kw1">for</span> <span class="br0">(</span>var i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0"><</span> arr<span class="sy0">.</span><span class="me1">Count</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">)</span>
<span class="br0">{</span>
Console<span class="sy0">.</span><span class="me1">WriteLine</span><span class="br0">(</span>arr<span class="br0">[</span>i<span class="br0">]</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="br0">}</span>
 
<span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">void</span> Print<span class="br0">(</span><span class="kw1">this</span> <span class="kw4">Double</span><span class="br0">[</span>,<span class="br0">]</span> arr<span class="br0">)</span>
<span class="br0">{</span>
Console<span class="sy0">.</span><span class="me1">WriteLine</span><span class="br0">(</span><span class="st0">"Print: Double 2D Array"</span><span class="br0">)</span><span class="sy0">;</span>
 
<span class="kw1">for</span> <span class="br0">(</span>var i <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> i <span class="sy0"><</span> arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="sy0">;</span> i<span class="sy0">++</span><span class="br0">)</span>
<span class="br0">{</span>
<span class="kw1">for</span> <span class="br0">(</span>var j <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span> j <span class="sy0"><</span> arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="sy0">;</span> j<span class="sy0">++</span><span class="br0">)</span>
<span class="br0">{</span>
Console<span class="sy0">.</span><span class="me1">Write</span><span class="br0">(</span><span class="st0">"{0}{1}"</span>, arr<span class="br0">[</span>i, j<span class="br0">]</span>, j <span class="sy0">+</span> <span class="nu0">1</span> <span class="sy0"><</span> arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span> <span class="sy0">?</span> <span class="st0">", "</span> <span class="sy0">:</span> Environment<span class="sy0">.</span><span class="me1">NewLine</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="br0">}</span>
<span class="br0">}</span>
 
<span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">Double</span><span class="br0">[</span><span class="br0">]</span> MaxPerColoums<span class="br0">(</span><span class="kw1">this</span> <span class="kw4">Double</span><span class="br0">[</span>,<span class="br0">]</span> arr<span class="br0">)</span>
<span class="br0">{</span>
<span class="kw1">return</span> <span class="br0">(</span>Enumerable<span class="sy0">.</span><span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">.</span><span class="kw1">Select</span><span class="br0">(</span>
col <span class="sy0">=></span> <span class="kw3">new</span> <span class="br0">{</span> col, rows <span class="sy0">=</span> Enumerable<span class="sy0">.</span><span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">}</span><span class="br0">)</span><span class="sy0">.</span><span class="kw1">Select</span><span class="br0">(</span>
@t <span class="sy0">=></span> @t<;span class="sy0">.</span><span class="me1">rows</span><span class="sy0">.</span><span class="me1">Max</span><span class="br0">(</span>row <span class="sy0">=></span> arr<span class="br0">[</span>row, @t<;span class="sy0">.</span><span class="me1">col</span><span class="br0">]</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">.</span><span class="me1">ToArray</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
 
<span class="kw1">public</span> <span class="kw1">static</span> <span class="kw4">Double</span><span class="br0">[</span><span class="br0">]</span> MaxPerRows<span class="br0">(</span><span class="kw1">this</span> <span class="kw4">Double</span><span class="br0">[</span>,<span class="br0">]</span> arr<span class="br0">)</span>
<span class="br0">{</span>
<span class="kw1">return</span> <span class="br0">(</span>Enumerable<span class="sy0">.</span><span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">.</span><span class="kw1">Select</span><span class="br0">(</span>
row <span class="sy0">=></span> <span class="kw3">new</span> <span class="br0">{</span> row, cols <span class="sy0">=</span> Enumerable<span class="sy0">.</span><span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr<span class="sy0">.</span><span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="br0">)</span> <span class="br0">}</span><span class="br0">)</span><span class="sy0">.</span><span class="kw1">Select</span><span class="br0">(</span>
@t <span class="sy0">=></span> @t<;span class="sy0">.</span><span class="me1">cols</span><span class="sy0">.</span><span class="me1">Max</span><span class="br0">(</span>col <span class="sy0">=></span> arr<span class="br0">[</span>@t<;span class="sy0">.</span><span class="me1">row</span>, col<span class="br0">]</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span><span class="sy0">.</span><span class="me1">ToArray</span><span class="br0">(</span><span class="br0">)</span><span class="sy0">;</span>
<span class="br0">}</span>
<span class="br0">}</span></pre></div></td></tr></table></div><div class='geshi_syntax'><table><tr><td><div class='code'><pre class="vbnet"><span class="co1">' VB.Net</span>
<span class="kw6">Dim</span> matrixData <span class="sy0">=</span> <span class="kw2">New</span> <span class="kw4">Double</span><span class="br0">(</span>,<span class="br0">)</span> <span class="br0">{</span>
<span class="br0">{</span><span class="nu0">2.22</span>, <span class="nu0">1.98</span>, <span class="nu0">3.96</span>, <span class="nu0">4.98</span>, <span class="nu0">2.29</span><span class="br0">}</span>,
<span class="br0">{</span><span class="nu0">0.62</span>, <span class="nu0">1.22</span>, <span class="nu0">3.22</span>, <span class="nu0">4.92</span>, <span class="nu0">3.77</span><span class="br0">}</span>,
<span class="br0">{</span><span class="nu0">2.83</span>, <span class="nu0">0.97</span>, <span class="nu0">3.98</span>, <span class="nu0">4.92</span>, <span class="nu0">5.88</span><span class="br0">}</span>
<span class="br0">}</span>
 
matrixData.<span class="kw2">Print</span><span class="br0">(</span><span class="br0">)</span>
matrixData.<span class="me1">Cast</span><span class="br0">(</span><span class="kw2">Of</span> <span class="kw4">Double</span><span class="br0">)</span>.<span class="me1">Max</span><span class="br0">(</span><span class="br0">)</span>.<span class="kw2">Print</span><span class="br0">(</span><span class="br0">)</span>
matrixData.<span class="me1">MaxPerRows</span><span class="br0">(</span><span class="br0">)</span>.<span class="kw2">Print</span><span class="br0">(</span><span class="br0">)</span>
matrixData.<span class="me1">MaxPerColoums</span><span class="br0">(</span><span class="br0">)</span>.<span class="kw2">Print</span><span class="br0">(</span><span class="br0">)</span>
 
<span class="co1">'Extension</span>
<span class="co1">'Imports System.Runtime.CompilerServices</span>
 
Module Ext
 
<Extension<span class="br0">(</span><span class="br0">)</span>> _
<span class="kw2">Public</span> <span class="kw6">Function</span> MaxPerColoums<span class="br0">(</span><span class="kw2">ByVal</span> arr <span class="kw2">As</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">(</span>,<span class="br0">)</span><span class="br0">)</span> <span class="kw2">As</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">(</span><span class="br0">)</span>
<span class="kw2">Return</span> <span class="br0">(</span>Enumerable.<span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="br0">)</span>.<span class="br0">[</span><span class="kw6">Select</span><span class="br0">]</span><span class="br0">(</span><span class="kw6">Function</span><span class="br0">(</span>col<span class="br0">)</span> <span class="kw2">New</span> <span class="kw6">With</span> <span class="br0">{</span> _
col, _
Key .<span class="me1">rows</span> <span class="sy0">=</span> Enumerable.<span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="br0">)</span> _
<span class="br0">}</span><span class="br0">)</span>.<span class="br0">[</span><span class="kw6">Select</span><span class="br0">]</span><span class="br0">(</span><span class="kw6">Function</span><span class="br0">(</span>t<span class="br0">)</span> t.<span class="me1">rows</span>.<span class="me1">Max</span><span class="br0">(</span><span class="kw6">Function</span><span class="br0">(</span>row<span class="br0">)</span> arr<span class="br0">(</span>row, t.<span class="me1">col</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span>.<span class="me1">ToArray</span><span class="br0">(</span><span class="br0">)</span>
<span class="kw6">End</span> <span class="kw6">Function</span>
 
<Extension<span class="br0">(</span><span class="br0">)</span>> _
<span class="kw2">Public</span> <span class="kw6">Function</span> MaxPerRows<span class="br0">(</span><span class="kw2">ByVal</span> arr <span class="kw2">As</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">(</span>,<span class="br0">)</span><span class="br0">)</span> <span class="kw2">As</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">(</span><span class="br0">)</span>
<span class="kw2">Return</span> <span class="br0">(</span>Enumerable.<span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span><span class="br0">)</span>.<span class="br0">[</span><span class="kw6">Select</span><span class="br0">]</span><span class="br0">(</span><span class="kw6">Function</span><span class="br0">(</span>row<span class="br0">)</span> <span class="kw2">New</span> <span class="kw6">With</span> <span class="br0">{</span> _
row, _
Key .<span class="me1">cols</span> <span class="sy0">=</span> Enumerable.<span class="me1">Range</span><span class="br0">(</span><span class="nu0">0</span>, arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span><span class="br0">)</span> _
<span class="br0">}</span><span class="br0">)</span>.<span class="br0">[</span><span class="kw6">Select</span><span class="br0">]</span><span class="br0">(</span><span class="kw6">Function</span><span class="br0">(</span>t<span class="br0">)</span> t.<span class="me1">cols</span>.<span class="me1">Max</span><span class="br0">(</span><span class="kw6">Function</span><span class="br0">(</span>col<span class="br0">)</span> arr<span class="br0">(</span>t.<span class="me1">row</span>, col<span class="br0">)</span><span class="br0">)</span><span class="br0">)</span><span class="br0">)</span>.<span class="me1">ToArray</span><span class="br0">(</span><span class="br0">)</span>
<span class="kw6">End</span> <span class="kw6">Function</span>
 
<Extension<span class="br0">(</span><span class="br0">)</span>> _
<span class="kw2">Public</span> <span class="kw6">Sub</span> <span class="kw2">Print</span><span class="br0">(</span><span class="kw2">ByVal</span> i <span class="kw2">As</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">)</span>
Console.<span class="me1">WriteLine</span><span class="br0">(</span><span class="st0">"Print: Double"</span><span class="br0">)</span>
Console.<span class="me1">WriteLine</span><span class="br0">(</span>i<span class="br0">)</span>
<span class="kw6">End</span> <span class="kw6">Sub</span>
 
<Extension<span class="br0">(</span><span class="br0">)</span>> _
<span class="kw2">Public</span> <span class="kw6">Sub</span> <span class="kw2">Print</span><span class="br0">(</span><span class="kw2">ByVal</span> arr <span class="kw2">As</span> IList<span class="br0">(</span><span class="kw2">Of</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">)</span><span class="br0">)</span>
Console.<span class="me1">WriteLine</span><span class="br0">(</span><span class="st0">"Print: Double Array"</span><span class="br0">)</span>
<span class="kw2">For</span> i <span class="kw2">As</span> Int32 <span class="sy0">=</span> <span class="nu0">0</span> <span class="kw2">To</span> arr.<span class="me1">Count</span><span class="br0">(</span><span class="br0">)</span> <span class="sy0">-</span> <span class="nu0">1</span>
Console.<span class="me1">WriteLine</span><span class="br0">(</span>arr<span class="br0">(</span>i<span class="br0">)</span><span class="br0">)</span>
<span class="kw2">Next</span>
<span class="kw6">End</span> <span class="kw6">Sub</span>
 
<Extension<span class="br0">(</span><span class="br0">)</span>> _
<span class="kw2">Public</span> <span class="kw6">Sub</span> <span class="kw2">Print</span><span class="br0">(</span><span class="kw2">ByVal</span> arr <span class="kw2">As</span> <span class="br0">[</span><span class="kw4">Double</span><span class="br0">]</span><span class="br0">(</span>,<span class="br0">)</span><span class="br0">)</span>
Console.<span class="me1">WriteLine</span><span class="br0">(</span><span class="st0">"Print: Double 2D Array"</span><span class="br0">)</span>
<span class="kw2">For</span> i <span class="kw2">As</span> Int32 <span class="sy0">=</span> <span class="nu0">0</span> <span class="kw2">To</span> arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">0</span><span class="br0">)</span> <span class="sy0">-</span> <span class="nu0">1</span>
<span class="kw2">For</span> j <span class="kw2">As</span> Int32 <span class="sy0">=</span> <span class="nu0">0</span> <span class="kw2">To</span> arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span> <span class="sy0">-</span> <span class="nu0">1</span>
Console.<span class="kw6">Write</span><span class="br0">(</span><span class="st0">"{0}{1}"</span>, arr<span class="br0">(</span>i, j<span class="br0">)</span>, <span class="kw6">If</span><span class="br0">(</span>j <span class="sy0">+</span> <span class="nu0">1</span> < arr.<span class="me1">GetLength</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span>, <span class="st0">", "</span>, Environment.<span class="kw1">NewLine</span><span class="br0">)</span><span class="br0">)</span>
<span class="kw2">Next</span>
<span class="kw2">Next</span>
<span class="kw6">End</span> <span class="kw6">Sub</span>
 
<span class="kw6">End</span> Module</pre></div></td></tr></table></div>
Sign In or Register to comment.