Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList element swapping not permanent
    text
    copied!<p>I have an arraylist of playing cards.</p> <pre><code>private ArrayList&lt;VisibleCard&gt; m_vecCards; </code></pre> <p>It has a private scope however upon accessing its members via:</p> <pre><code>public VisibleCard getCard(int n) { return this.m_vecCards.get(n); } </code></pre> <p>I find that the elements are unsorted via the bubble sort algorithm:</p> <pre><code>public void sortCards() { int swapCounter, nIndex; int upperBound = this.m_vecCards.size() - 1; while(true) { swapCounter = 0; for ( nIndex = 0; nIndex &lt; upperBound; nIndex++ ) { if ( this.m_vecCards.get(nIndex).getNumericVal() &gt; this.m_vecCards.get(nIndex + 1).getNumericVal() ) { Collections.swap(this.m_vecCards, nIndex, nIndex + 1); swapCounter++; } } if ( swapCounter == 0 ) { for ( nIndex = 0; nIndex &lt; 7; nIndex++ ) { Log.d("order", ""+ this.m_vecCards.get(nIndex).getNumericVal()); } return; } } } </code></pre> <p>I have a log function I can use to test the numbers to ensure my arrays are being sorted and it is the case that the array is being sorted, the results however are not permanent.</p> <pre><code>12-18 06:51:08.077: D/order(3636): 2 12-18 06:51:08.077: D/order(3636): 4 12-18 06:51:08.077: D/order(3636): 5 12-18 06:51:08.077: D/order(3636): 8 12-18 06:51:08.077: D/order(3636): 8 12-18 06:51:08.077: D/order(3636): 9 12-18 06:51:08.077: D/order(3636): 9 12-18 06:51:08.087: D/order(3636): 1 12-18 06:51:08.087: D/order(3636): 5 12-18 06:51:08.087: D/order(3636): 6 12-18 06:51:08.087: D/order(3636): 8 12-18 06:51:08.087: D/order(3636): 12 12-18 06:51:08.087: D/order(3636): 12 12-18 06:51:08.087: D/order(3636): 13 12-18 06:51:08.087: D/order(3636): 1 12-18 06:51:08.087: D/order(3636): 1 12-18 06:51:08.087: D/order(3636): 2 12-18 06:51:08.087: D/order(3636): 3 12-18 06:51:08.087: D/order(3636): 3 12-18 06:51:08.087: D/order(3636): 10 12-18 06:51:08.087: D/order(3636): 10 12-18 06:51:08.087: D/order(3636): 4 12-18 06:51:08.097: D/order(3636): 6 12-18 06:51:08.097: D/order(3636): 6 12-18 06:51:08.097: D/order(3636): 9 12-18 06:51:08.097: D/order(3636): 10 12-18 06:51:08.097: D/order(3636): 11 12-18 06:51:08.097: D/order(3636): 13 </code></pre> <p>Essentially the elements within the ArrayList are not being swapped "permanently" as I would like. I cannot understand this phenomena as I see nothing wrong with the code or the log debugging.</p> <pre><code>public void addCard(VisibleCard vc) { if ( this.m_humanAgent ) { vc.setPosition(-20, (this.m_vecCards.size() * 85) + 100); vc.rotate(1.57079633); vc.setup(); } this.m_vecCards.add(vc); if ( this.m_vecCards.size() == 7 ) { this.sortCards(); } } </code></pre> <p>I am adding a total of seven cards for each player and after seven elements exist within the array, I invoke the sortCards() method but after outputting the cards, they do not appear in the order they ought to.</p> <p>I render the output like this:</p> <pre><code> for ( int n = 0; n &lt; 7; n++ ) { this.m_flushRummy.getPlayer(0).getCard(n).draw(gl, m_bmTextureIds[0]); } </code></pre>
 

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