Populating a Boolean Array in Java

Solution 1:

The most efficient and expressive way is the following:

Use enums as Exits and use an EnumSet to store them. EnumSet is an efficient Set implementation that uses a bit field to represent the enum constants.

Here is how you can do it:

public enum Exit { North, West, South, East, Up, Down; }

EnumSet<Exit> set = EnumSet.noneOf(Exit.class); // An empty set.

// Now you can simply add or remove exits, everything will be stored compactly

set.add(Exit.North); // Add exit
set.contains(Exit.West); // Test if an exit is present
set.remove(Exit.South); //Remove an exit

Enum set will store all exits in a single long internally, so your code is expressive, fast, and saves a lot of memory.

Solution 2:

Is there any reason why you are doing this with Strings and aren't passing in booleans, i.e.

public void setExits(boolean N, boolean E, boolean S, boolean W, boolean U, boolean D) 

Or having setters?

public void setNorthOpen(boolean open)
{
  bexits[4] = open;
}

Secondly, why are you storing the exits as an array of booleans, it's a small finite set, why not just

boolean N,S,E,W,U,D;

As then you don't need to keep track of which number in the array each direction is.

Also

This is a correct answer (if not completely optimal like that of @gexicide) but I fully encourage anyone to look at the other answers here for an interesting look at how things can be done in Java in different ways.

For future reference

Code which works belongs on Code Review, not Stack Overflow. Although as @kajacx pointed out, this code shouldn't -in fact- work.

Solution 3:

OK, first of all, your setExits() method will not work as intended, chained if-elseif will maximally execute 1 branch of code, for example:

if (e.contains("N"))
    bexits[0] = true;
else if (e.contains("W"))
    bexits[1] = true;

Even if e contains both N and W, only bexits[0] will be set. Also this method will only add exits (for example calling setExits("") will not delete any existing exits.

I would change that method to:

bexits[0] = e.contains("N");
bexits[1] = e.contains("W");
...

Also, i definetly wouldn't remember that north is on index 0, west in on 1, ... so a common practice is to name your indexes using final static constants:

public static final int NORTH = 0;
public static final int WEST = 1;
...

Then you can write in your setExits method:

bexits[NORTH] = e.contains("N");
bexits[WEST] = e.contains("W");
...

(much more readible)

Finally, if you want your code even more well-arranged, you can make a Exits class representing avaliable exits, and backed by boolean array. Then on place where you create your String, you could create this class instead and save yourself work with generating and then parsing a string.

EDIT:

as @gexicide answers, there is a really handy class EnumSet which would be probably better for representing the exits than bollean array.