0

I have made a class called Iset that takes integers and modifies it's boolean array's index equivalent to the integer to true.

e.g. If I pass an integer 1 then the boolean array setI[1] is turned to true.

I have a method called include that returns true if the provided integer is in there and false if it isn't. However no matter what I do I always get true. I have made sure that everything in the array is set to false and I add in a number further up the code. Obviously I'm missing something really obvious here:

public class Iset {
    public int size;
    boolean[] setI;

    Iset(int a) {
        this.size = a;
        this.setI = new boolean[size];
    }

    public boolean include(int i) {
        for (int n = 0; n <= size; n++) {
            if (setI[n]== setI[i]){
                return true;
            }
        }
        return false;
    }
}
4
  • 2
    What is the type of setI? What is size? Show us the code that fills setI. Commented Feb 10, 2014 at 9:04
  • takes integers and stores them in a boolean array you should rethink this. What is about an int array ? Commented Feb 10, 2014 at 9:06
  • 1
    why are you storing integers in a boolean array? And how exactly are you doing that? Commented Feb 10, 2014 at 9:06
  • @Rohit I made the edit... It was slightly wrong to state "storing integers in boolean array" Commented Feb 10, 2014 at 9:19

5 Answers 5

3

Please try this code, I think you should add a funktion: set(), and change a little of the include(int i)

public class Iset {
public int size;
boolean[] setI;

Iset(int a) {
    this.size = a;
    this.setI = new boolean[size];
}

public boolean include(int i) {

    return setI[i];
}

    //set your values in the boolean array "setI" to "true" 

public void set(int... values) {

    for (int i : values) {
        setI[i] = true;

    }

}

public static void main(String[] args) {

    Iset mm = new Iset(100);

    mm.set(25, 40, 22);

    System.out.println(mm.include(25));

}

}

Sign up to request clarification or add additional context in comments.

Comments

3

The other answers have given solutions, but I think we can also get an explanation going as to why your original code was slightly wrong as you say. Here is what your include() method is doing in pseudocode:

for each boolean called 'setI[n]' in the array:
    if 'setI[n]' is the same as the boolean at 'setI[i]':
        return true

So, it's not actually checking if either of those boolean are true or false, it's just checking if they are the same. This method will always return true unless the boolean at index i is the only one in the array with its value (I'd suggest trying that to see if I am right). For example, if i = 1 your method will return true for:

[false, true, false, false, ...]
[true, false, true, true, ...]

... and no other values.

Hopefully this makes things a little clearer.

1 Comment

Thank you for reinforcing the ideas laid out. Makes a lot more sense now.
2

You don't have to walk over the complete array, just ask the method if your number is included.

public boolean isIncluded(int i) {
    if (setI[i] == true){
        return true;
    }
    return false;
}

or even simpler:

public boolean isIncluded(int i) {
    return setI[i];
}

P.S. I changed your method name to something more meaningful

3 Comments

contains might be even better, since that's what's used in the Collections APIs.
in general yes, but we do not know what the context is, it may be that "isIncluded" makes more sense ;)
That's a very straight forward way of doing it. Problem is when I think too hard at something I just end up making it more and more complicated. Thank you
0

Try this:

public boolean include(int i) {
    if (i >= size){
        // To avoid ArrayIndexOutOfBoundsException
        return false;
    }
    return setI[i];
}

Comments

0

I'm not completely sure what you are after for, but, in you for-loop you are making a selfcomparison when n == i and thus return true always.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.