java - Short|quick int values comparison -
i learned terminary expression, want little different.
i have following:
int mode = getmyintvalue();
i comparison following:
if(mode == 1 || mode == 2 || mode == 3) //do
i know if there short way of doing this, tried didn't work:
if(mode == 1 || 2 || 3) //do
there short|quick way of doing it? quick "ifs" because makes code more clear, example, more clear this:
system.out.println(mode == 1 ? text1 : text2):
than this:
if(mode == 1) system.out.println(text1): else system.out.println(text1):
thanks in advance!
well, if don't mind boxing hit, use set prepared earlier:
// use more appropriate name if necessary private static final set<integer> valid_modes = new hashset<>(arrays.aslist(1, 2, 3)); ... if (valid_modes.contains(mode)) { }
you use int[]
, custom "does array contain value" method if wanted... o(n) or o(log n) binary search, suspect we're talking small sets anyway.
Comments
Post a Comment