C#, check whether integer array has negative numbers in it
I have a array int[] numArray
. I want to know is there any straight forward way to just check whether array has negative numbers in it ?
If there is no direct method 开发者_StackOverflow社区even linq will do . I am bit new to linq . Can anyone suggest ?
If you're open to using LINQ:
var containsNegatives = numArray.Any(n => n < 0);
Or, if you want to do it the "old fashioned" way...you just have to loop:
var containsNegatives = false;
foreach(var n in numArray)
{
if(n < 0)
{
containsNegatives = true;
break;
}
}
And if you really want to get fancy, you could turn that into an Extension method:
public static class EnumerableExtensions
{
public static bool ContainsNegatives(this IEnumerable<int> numbers)
{
foreach(n in numbers)
{
if(n < 0) return true;
}
return false;
}
}
And call it from your code like:
var containsNegatives = numArray.ContainsNegatives();
You could use Any:
bool containsNegative = numArray.Any(i => i < 0)
Or
bool containsNegative = numArray.Min() < 0;
EDIT
int[] negativeNumbers = numArray.Where(i => i < 0).ToArray();
var negativeExist = numArray.Any(a => a < 0);
You can use Array.Find(T) method to perform this task.
public static T Find<T>(
T[] array,
Predicate<T> match
)
For example,
using System;
using System.Drawing;
public class Example
{
public static void Main()
{
// Create an array of five Point structures.
Point[] points = { new Point(100, 200),
new Point(150, 250), new Point(250, 375),
new Point(275, 395), new Point(295, 450) };
// To find the first Point structure for which X times Y
// is greater than 100000, pass the array and a delegate
// that represents the ProductGT10 method to the static
// Find method of the Array class.
Point first = Array.Find(points, ProductGT10);
// Note that you do not need to create the delegate
// explicitly, or to specify the type parameter of the
// generic method, because the C# compiler has enough
// context to determine that information for you.
// Display the first structure found.
Console.WriteLine("Found: X = {0}, Y = {1}", first.X, first.Y);
}
// This method implements the test condition for the Find
// method.
private static bool ProductGT10(Point p)
{
if (p.X * p.Y > 100000)
{
return true;
}
else
{
return false;
}
}
}
/* This code example produces the following output:
Found: X = 275, Y = 395
*/
Traditional:
foreach (int number in numArray) { if (number < 0) return true; }
return false;
With LINQ:
bool result = numArray.Any(x => x < 0);
A bit twidling version would be
public static bool AnyNegative(int[] arr){
const long firstBit = 2147483648;
var res = false;
for (var i = 0; i < arr.Length && !res; i++) res = (arr[i] & firstBit) == firstBit;
return res;
}
you can the call it like this>
int arr = {...}
if(arr.AnyNegative()){
//do stuf if there's any negative numbers
}
of course this is just an obfuscated version of
public static bool AnyNegative(int[] arr){
var res = false;
for (var i = 0; i < arr.Length && !res; i++) res = arr[i] < 0;
return res;
}
精彩评论