I have a dilemma I am trying to find out the best way to compare a string variable called (code) to an array of strings. if it equal it i want it to break the for loop. Which would should I select. I think the 2nd one will work but the 1st one seems like it would and its simpler. Any advice would be appreciated.
String[] badcodes = {"8QQ", "8BQ", "8JQ"}; if (code.equals(badcodes)) { break; } String[] badcodess = {"8QQ", "8BQ", "8JQ"}; for (String s : badcodess) { if (s.equals(code)) { break; // break out of for loop } } --------------based on answer ----------------------
String[] badcodes = {"8QQ", "8BQ", "8JQ"}; boolean check = Arrays.asList(badcodess).contains(code); if (check = true) { // do something } else { // do something }
code?Stringwill never be equal to aString[]