-6

I have a two-dimensionial array of nullable integers. I wanna get the non-nullable corners of that array - so like, the corners of the array below would be 13, 76, 39, 87.

int?[,] array = new int?[3,9]
{
    { null ,   13 , 21   , null , null ,   52 ,   69 , 76 , null },
    {    9 ,   15 , null ,   36 ,   45 , null , null , 77 , null },
    { null , null , null ,   39 ,   48 ,   53 , null , 79 ,   87 },
};
just-a-hriday
  • 148
  • 1
  • 13
  • 1
    Do you expect {{13, 76}, {39,87}} back? – Hayden Aug 05 '21 at 05:53
  • You can get the upper dimensions overall from [here](https://stackoverflow.com/questions/4260207/how-do-you-get-the-width-and-height-of-a-multi-dimensional-array) but you'll need to iterate through to find your results. Note that your question title doesn't describe what you're doing with any clarity: you are not looking for the corners. The word "corner" evokes the idea of 0,0 0,8 2,0 2,8, which isn't what you're asking for. – ProgrammingLlama Aug 05 '21 at 05:59
  • 1
    Does this answer your question? [How to get a complete row or column from 2D array in C#](https://stackoverflow.com/questions/27427527/) and [How to get index using LINQ?](https://stackoverflow.com/questions/2471588/) and [Find index of a value in an array](https://stackoverflow.com/questions/1764970/) and [c# Array.FindAllIndexOf which FindAll IndexOf](https://stackoverflow.com/questions/10443461/) and [Get the first and last item of an array](https://stackoverflow.com/questions/7387396/) –  Aug 05 '21 at 06:11
  • Down-voters who might be thinking this is easy please note _"[an array **is not a rectangle** - it is a single linear space. Any concept of 'row' or 'column' is actually the invention of the user. There are no rows and no columns; any convention along the lines of ... is 'row x, column y' or 'column x, row y' is purely that: a convention; part of our imagination in conceptualizing something. The only real order is **which index is navigated first**. The first? or the last?](https://stackoverflow.com/questions/17025551/array-copy-always-row-major)"_ , Marc Gravell, June 2013. –  Aug 05 '21 at 06:52
  • @Hayden exactly. – just-a-hriday Aug 06 '21 at 02:52

1 Answers1

2

From that, I want to get both the first and last non-null elements from the first and last rows each.

2D arrays are a pain in the ass, but they behave like one long array if you iterate them

var w = array.GetLength(1);
var h = array.GetLength(0);
var a = array.Cast<int?>();

var ff = a.First(e => e.HasValue);
var fl = a.Take(w).Last(e => e.HasValue);
var lf = a.Skip((h-1)*w).First(e => e.HasValue);
var ll = a.Last(e => e.HasValue);

Width is 9, Height is 3, Casting turns it into an enumerable of int? which means your corners are the first non null, the last non null in the initial 9, the first non null after skipping 18 and the last non null

Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • 1
    _"2D arrays are a pain in the ass"_ - lol. Yes I was surprised C# wasn't _row-major_ at least. –  Aug 05 '21 at 06:54