here is the issue, I have an array defined like below:
int[,] Users = new int[1000,3];
Its data will be something like:
the array is used as needed by my script. but I need to be able to filter the array based on one of its dimensions, and return all available matches.
For example, filtering on dimension [1], wanting all that match '3' will return an array containing:
Could anyone give me a hand with this?
Many thanks.
Size of multidimensional arrays: The total number of elements that can be stored in a multidimensional array can be calculated by multiplying the size of all the dimensions. For example: The array int[][] x = new int[10][20] can store a total of (10*20) = 200 elements.
6) What is the output of C Program with arrays.? Explanation: 0 0 is the answer.
To pass multidimensional arrays to a function, only the name of the array is passed to the function (similar to one-dimensional arrays).
The basic form of declaring a two-dimensional array of size x, y: Syntax: data_type array_name[x][y]; Here, data_type is the type of data to be stored.
If you can change your array from int[,]
to int[][]
then you can easily achieve this using LINQ.
int[][] users = new int[][]
{
new int[]{0,1,2},
new int[]{1,2,1},
new int[]{2,3,2},
new int[]{3,3,4},
new int[]{4,2,3}
};
var result = from u in users
where u[1] == 3
select u;
If changing your array is not an option then you could write a Filter
function as follows.
public static IEnumerable<T[]> Filter<T>(T[,] source, Func<T[], bool> predicate)
{
for (int i = 0; i < source.GetLength(0); ++i)
{
T[] values = new T[source.GetLength(1)];
for (int j = 0; j < values.Length; ++j)
{
values[j] = source[i, j];
}
if (predicate(values))
{
yield return values;
}
}
}
The above could then be called as follows
var result = Filter(users, u => u[1] == 3);
You could take this a step further and implement your own custom Linq extension for the Where
function which would allow you to filter the T[,]
arrays. Here is a naive example that could get you started.
public static class LinqExtensions
{
public static IEnumerable<T[]> Where<T>(this T[,] source, Func<T[], bool> predicate)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
return WhereImpl(source, predicate);
}
private static IEnumerable<T[]> WhereImpl<T>(this T[,] source, Func<T[], bool> predicate)
{
for (int i = 0; i < source.GetLength(0); ++i)
{
T[] values = new T[source.GetLength(1)];
for (int j = 0; j < values.Length; ++j)
{
values[j] = source[i, j];
}
if (predicate(values))
{
yield return values;
}
}
}
}
With this you can again use Linq as in the first example to filter the array
var result = from u in users
where u[1] == 3
select u;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With