java - For Loop is skipping every other line while searching ArrayList -


i have array list of sequential integers 10 100. generate random number (say 50) , want remove numbers have "5" in 10's position- integers 50 - 59.

public static void updatemyguess() {   arraylist<integer> possibles = new arraylist <integer>();   (int = 10; < 100; i++) {    possibles.add(i);      }   int guess = 50; //my guess generated method. not listed here  int tens = guess / 10 % 10;  int x;   ( int = 0; < possibles.size(); i++) {    x = possibles.get(i);    if (x / 10 % 10 == tens) {      possibles.remove(i);  }  } 

for reason remove every other value arraylist in 50's: 50, 52, 54... 58. why this? , can change make take out 50s?

using iterator suggested @nlloyd better solution here.

however, there might occasion (for whatever reason) can't use iterator (e.g. working in language or library doesn't provide iterators). simple solution iterate in reverse:

for ( int = possibles.size()-1; >= 0; i--) { 

it isn't clear why need add items list in first place if going remove them again immediately:

 arraylist<integer> possibles = new arraylist <integer>();  int guess = 50; //my guess generated method. not listed here  int tens = guess / 10 % 10;   (int x = 10; x < 100; x++) {    if (x / 10 % 10 != tens) {      possibles.add(x);    }   } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -