Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Populating a Boolean Array in Java

Tags:

java

arrays

As a fairly green Java coder I've set myself the hefty challenge of trying to write a simple text adventure. Unsurprisingly, I've encountered difficulties already!

I'm trying to give my Location class a property to store which exits it contains. I've used a boolean array for this, to essentially hold true/false values representing each exit. I'm not entirely convinced that

a) this is the most efficient way to do this and

b) that I'm using the right code to populate the array.

I would appreciate any and all feedback, even if it is for a complete code over-haul!

At present, when instantiating a Location I generate a String which I send through to the setExits method:

    String e = "N S U";
    secretRoom.setExits(e);

In the Location class, setExits looks like this:

public void setExits(String e) {
    if (e.contains("N"))
        bexits[0] = true;
    else if (e.contains("W"))
        bexits[1] = true;
    else if (e.contains("S"))
        bexits[2] = true;
    else if (e.contains("E"))
        bexits[3] = true;
    else if (e.contains("U"))
        bexits[4] = true;
    else if (e.contains("D"))
        bexits[5] = true;
}

I'll be honest, I think this looks particularly clunky, but I couldn't think of another way to do it. I'm also not entirely sure now how to write the getExits method...

Any help would be welcome!

like image 237
lordchancellor Avatar asked Jul 09 '14 10:07

lordchancellor


People also ask

How do you fill a boolean array in Java?

Either use boolean[] instead so that all values defaults to false : boolean[] array = new boolean[size]; Or use Arrays#fill() to fill the entire array with Boolean.

How do you make a boolean array?

A boolean array can be created manually by using dtype=bool when creating the array. Values other than 0 , None , False or empty strings are considered True. Alternatively, numpy automatically creates a boolean array when comparisons are made between arrays and scalars or between arrays of the same shape.

How do you initialize a boolean list in Java?

boolean[] arr = new boolean[10]; This will auto-initialize to false since boolean 's default value is false .

How do you make a 2d boolean array in Java?

grid = new boolean[n][n]; Simply typing the above line of code would result in the creation of a (n x n) 2-D array with default values set to false. However, the complexity remains O(n^2) with or without the usage of 2 loops(nested).


3 Answers

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.

like image 86
gexicide Avatar answered Sep 27 '22 17:09

gexicide


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.

like image 45
Ross Drew Avatar answered Sep 27 '22 17:09

Ross Drew


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.

like image 24
kajacx Avatar answered Sep 27 '22 19:09

kajacx