tags:

views:

79

answers:

6

Hello.

Assume I declared an Array isFind[] like this,

bool[] isFind = new bool[5];

for (int i = 0; i < 5; ++i)
{
   isFind[i] = init(); // initialize isFind[]
}

bool init();
{
  ...; // some code here
}

Is there any quick method (less typing code) to detect there is false element or not in isFind[]? I don't want to for/foreach Loop each element in the array list.

[Update]

.NET 2.0 Used.

quick method (less typing code)

+2  A: 

If you have .NET 3.5 or higher you can use Enumerable.Any:

isFind.Any(x => !x)

From your update to the question you are using .NET 2.0 so writing out the loop is probably the best option.

Mark Byers
@Mark, I am not sure I can use `Any()` or not in .NET 2.0. I tried the way in VS2005 just now, but I can't find it. Thank you.
Nano HE
Just performance point: `Any<T>` does foreach internally so it is only a code shortcut, not a performance shortcut.
Jaroslav Jandek
+6  A: 

If you're using .NET2 then you can use the static TrueForAll method:

bool allTrue = Array.TrueForAll(isFind, delegate(bool b) { return b; });
LukeH
+1 for showing `Array.TrueForAll` method (I never noticed there was such)
Regent
Nano HE
@All, Thanks a lot.
Nano HE
+1. Never used that one, goot tip. `Array.IndexOf` is shorter and faster, though ;)
Jaroslav Jandek
@Jaroslav: Yes, in this case `IndexOf` will be marginally faster. But in cases where the predicate is more complicated than just a straight match then you'd have to use `TrueForAll` rather than `IndexOf`.
LukeH
+1  A: 

This code does not use a seperate loop to find false elements. (There is in general, no way to perform comparison on n objects without looping over all of them at least once.) For a five member array, loop cost is negligible. If you are using bigger arrays, look at BitArray for performance/space benefits.

bool isFind[] = new isFind[5];
bool falseExists = false;

for (int i = 0; i < 5; ++i)
{
   isFind[i] = init(); // initialize isFind[]
   if(!isFind[i])
       falseExists=true;
}

bool init();
{
  ...; // some code here
}
apoorv020
+1  A: 

I think you can record whether any element was false during initialization phase:

bool isFind[] = new isFind[5];
bool containsFalse = false;

for (int i = 0; i < isFind.Length; i++)
{
   if (!(isFind[i] = init())) // initialize isFind[]                                 
      containsFalse = true; // and record if item was false
}
Regent
+2  A: 

.NET 2.0 version:

bool []isFind = new bool[3];

isFind[0] = true;
isFind[1] = true;
isFind[1] = false;

bool exists = (Array.IndexOf(isFind, false) != -1);

The code uses an internal for loop. Also you had the array declaration wrong.

Jaroslav Jandek
I'm just curious, what was wrong in his array declaration?
Regent
It used to be `bool isFind[] = new isFind[5];` (as copied in other answers).
Jaroslav Jandek
+2  A: 

The only way to find something is to look for it. Assuming that your array is not sorted (if it were, you would only need to look at the first or last element, depending on the sorting), and you have no external cache, you have to check all its elements, so you need a loop. It does not matter if you write it explicitly or through another function, the loop will be there somehow.

Gorpik
+1. Thanks for teaching.
Nano HE
You don't always have to check all the elements. If you find one that is false then you don't need to check the rest.
Mark Byers
@Mark: Well, I took that for granted. But you have to be prepared to check them all in case you don't find the one you are looking for. And you have to loop, no matter whether you stop soon or not.
Gorpik
+1, the loop will be definitely somewhere, but in some cases existing loops can be reused. (Like checking for false in initialization loop rather than creating a new loop just for checking.)
Regent
@Regent: You are right. If you know that checking for any `false` value in the array will be important, using a external cache (as you propose in your own answer) is a good idea. The more you know about the potential uses of your class, the better you can prepare for them and the happier your clients will be.
Gorpik