Note that there are some explanatory texts on larger screens.

plurals
  1. POArrayList accessing an element that should have been deleted
    primarykey
    data
    text
    <p>I'm using Java for a data mining project and am having an odd issue with an ArrayList.</p> <p>The ArrayList (availAttribs) contains the names of all the attributes not yet used in the current branch of a decision tree. At the beginning of the loop an attribute is chosen and the index of that attribute in the ArrayList is stored in chosenAttribute. The logic is performed then the item is removed from the list and another is chosen.</p> <p>The program wasn't behaving as expected so I used a for each loop to display the current contents of the ArrayList before each attribute selection so I could check the contents.</p> <p>This is what I got:</p> <pre><code>availAttribs contains ... -Color -size -act -age Chose attribute #1: size availAttribs contains ... -Color -act -age Chose attribute #1: size </code></pre> <p>The attributes are being chosen at random:</p> <pre><code>for(String s : availAttribs) { System.out.println(" -" + s); } chosenAttribute = random.nextInt(availAttribs.size() - 1); System.out.println(" Chose attribute #" + chosenAttribute + ": " + trainSet.attribute(chosenAttribute).name()); // other stuff here availAttribs.remove(chosenAttribute); </code></pre> <p>All of that is within a while loop checking for the exit condition. I would think that if an item wasn't successfully removed that it would show up when I iterated through the list and printed each element but apparently it can still access them.</p> <p>Edit: random was declared outside the while loop:</p> <pre><code>Random random = new Random(System.currentTimeMillis() ); </code></pre>
    singulars
    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.
 

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