简体   繁体   中英

Java ternary operator with multiple conditions

I'm fairly new to Java data-structures and I need some assistance to have multiple conditions using ternary operator or if there's much better approach on what I'm trying to do.

Basically, I have a text file that put into a 2D array and splitting each line character by character.

However, I'm having some trouble with conditional statement to check specific characters from the line.

Here's my current algorithm to do that.

for (int i = 2; i < lines.size() - 1; i++) {
        String floorLine = lines.get(i);
        int j = 0;
        for(char coor : floorLine.toCharArray()) {
              floor[i - 2][j] = (coor == '#') ? 0 : 1;
        }
    }

As of now, I'm only checking if there's # in line then it will encode as 0 else 1 . However, I would like to have something like this

floor[i-2][j] = (coor == '#') ? 0 floor[i-2][j] = (coor == 'X') ? 1 floor[i-2][j] = (coor == 'Z') ? 2 : 3

So if I use the normal if else statement, I should have like this

if 2d array == ( coor == '#') encode as 0
if 2d array == ( coor == 'X') encode as 1
if 2d array == ( coor == 'Z') encode as 2
else encode as 3

Any suggestions or tips will be appreciated! Thank you in advance.

May as well throw in how I would write this. Pull the conditional into a new method.

floor[i - 2][j] = getNumberForCoor(coor);



private static int getNumberForCoor(char coor) {
    switch (coor) {
        case '#': return 0;
        case 'X': return 1;
        case 'Z': return 2;
        default:  return 3;
    }
}

The form of the conditional expression is:

condition? value_if_true : value_if_false

When they're chained, it looks like this:

condition_1 ? value_if_1 : condition_2 ? value_if_2 : value_otherwise

So I think you mean:

floor[i-2][j] = (coor=='#' ? 0 : coor=='X' ? 1 : coor=='Z' ? 2 : 3);

To me, this is simple enough to be perfectly readable, but lots of people argue against this use of conditional operator like this; and if it were any more complicated you would be better off using if statements instead.

Java is in the process of introducing switch expressions , which would also work well for this case.

Besides the ternary expression chain I see two alternatives:

  • A precalculated mapping from the char to an int.
  • If the result is a consecutive numbering 0, 1, 2, ... then the index from the char in a string is usable.

So

Map<Character, Integer> map = new HashMap<>();
map.put('#', 0);
map.put('X', 1);
map.put('!', 2);

String enc = "#X!";

for (int i = 2; i < lines.size() - 1; i++) {
    String floorLine = lines.get(i);
    int j = 0;
    for(char coor : floorLine.toCharArray()) {
          floor[i - 2][j] = coor == '#' ? 0
                 : coor == 'X' ? 1
                 : coor == '!' ? 2
                 : 3;

          floor[i - 2][j] = map.getOrDefault(coor, 3);

          int e = enc.indexOf(coor);
          floor[i - 2][j] = e == -1 ? 3 : e;
    }
}

Looks like you could put your replacements in a map.

static Map<Character, Integer> encodings = new HashMap<>();

static {
    // static initializer, a way to populate the map easily, not the only way
    encodings.put('#', 0);
    encodings.put('X', 1);
    encodings.put('Z', 2);
}

//and in your function
floor[i-2][j] = encodings.getOrDefault(coor, 3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM