-1

Say I have a char array, I wanna classify its element into three states. If it's a number, then mark it as State 1, if it's a operator, then mark it as State 2, if else, mark it State 3.

First, I initialize a boolean array to represent its state, but I fount it only can store two states:(

Then I switch to int array, yet this way is kinda a waste of space.

Could you offer better idea please?

DoubleX
  • 351
  • 3
  • 18

2 Answers2

2

if you need to represent something like the state of a finite state machine, or Values of an hypothetical class Card, you should use enum, something like

public class EnumTest {

    public enum State{
        START_STATE("1"),INTER_STATE("2"),END_STATE("3");

        String name;
        State(String name){this.name = name;}
        String getName() {return name;}
    }

    public static void main(String[] args) {
        for(State state : EnumTest.State.values())
            System.out.println(state.getName());
    }
}

PS: Constructor and fields are actually optional if space matters: your enum can also be

public enum{FIRST,SECOND,THIRD;}
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
majik
  • 128
  • 9
1

According to this SO page: https://stackoverflow.com/questions/952169/what-is-the-third-boolean-state-in-java, you could also use a wrapped Boolean defaulting to null, which could potentially be used as a third type if you do not reassign the boolean to any other value.

Egzy
  • 43
  • 8