Note that there are some explanatory texts on larger screens.

plurals
  1. POJava enum and loop question
    primarykey
    data
    text
    <p>I have a list of Nodes which I loop through:</p> <pre><code>for (int i = 0; i &lt; aNodes.getLength(); i++) { // } </code></pre> <p>For example, assume the list contains 12 items, and that I know 3 items represent a row in a table, and the next 3 belong to the following row. That means my list of 12 Nodes originates from an HTML table containing 4 rows.</p> <p>After each row I want to do something, for example, create a new object and populate it...or whatever. I have a solution:</p> <pre><code>ArrayList&lt;Account&gt; mAccounts = new ArrayList&lt;Account&gt;(); Account account = new Account(); for (int i = 0; i &lt; aNodes.getLength(); i++) { String nodeValue = aNodes.item(i).getNodeValue(); boolean isNewRow = (i % COLS_PER_ROW == 0); if (isNewRow) { account = new Account(); mAccounts.add(account); } switch (i % COLS_PER_ROW) { case ACCOUNT_POS: account.setAccount(nodeValue); break; case BALANCE_POS: account.setBalance(nodeValue); break; case DATE_POS: account.setDate(nodeValue); break; } } </code></pre> <p>But there are numerous things I don't like about this solution:</p> <ol> <li>An Account instance is created twice first time, once outside the loop and then once because a new row is detected.</li> <li>It uses integer constants ACCOUNT_POS=0, BALANCE_POS=1, DATE_POS=2...this doesn't feel very nice and I think I should be using an enum.</li> <li>I can't use an enum with the loop variable 'i'.</li> <li>I can't use a for each loop since Nodes doesn't implement the correct interface,</li> </ol> <p>Can anyone suggest a better way of doing this that solves the list of things I don't like about it?</p> <p>Thank you.</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. 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