-2

So I have a 2D array of bytes ranging from 0 to 4 which are all jumbled up. What I want to do is specify a byte value, for example, 3. Then I want to get the offset (x and y location) of every 3 in the array and put them in a list (or another array) so I can iterate through all the 3s.

I'm pretty sure this is possible, I just don't know how to do it. I'm aware that there is no prebuilt method for iterating through multidimensional arrays so I'll have to write a method to do that.

To summarise: Filter all bytes of a certain type from a 2D array and put their coordinates (offsets) in a list or another array.

Pyroglyph
  • 164
  • 5
  • 14
  • 4
    _so I'll have to write a method to do that_, so do that! Show us what you have tried so far, and if you have a specific question, we will be happy to help you :) – Eminem May 26 '15 at 15:57
  • Please don't just ask us to solve the problem for you. Show us how _you_ tried to solve the problem yourself, then show us _exactly_ what the result was, and tell us why you feel it didn't work. See "[What Have You Tried?](http://whathaveyoutried.com/)" for an excellent article that you _really need to read_. – John Saunders May 26 '15 at 15:58
  • start by using a jagged array, its just better. – Jodrell May 26 '15 at 15:58
  • http://stackoverflow.com/questions/641499/convert-2-dimensional-array – Jodrell May 26 '15 at 16:00
  • What does your method for iterating through the multidimensional array look like? What is wrong with it? – Martijn May 26 '15 at 16:07
  • Thanks for all the fast answers, Eminem, I'm trying to write one but I'm just really confused about my own code :P. Jodrell, Why is a jagged array better? Martijn, all my code is at https://github.com/Pyroglyph/PlasmaticRevolution, Tile.cs and Map.cs are the main ones that relate to this question. – Pyroglyph May 26 '15 at 16:47
  • Okay so I added Diogo's code from the answer below but now it compiles fine but does nothing. It doesn't even draw the CornflowerBlue background that normally comes up in XNA. – Pyroglyph May 26 '15 at 17:23

1 Answers1

1
public class Vector2
{
    public int X { get; set; }
    public int Y { get; set; }
}


 public List<Vector2> GetBytes(byte[,] array, byte value)
    {
        List<Vector2> list = new List<Vector2>();
        int count;
        for (int i = 0; i < XSize; i++)
            for (int k = 0; k < YSize; k++)
                if (array[i, k] == value)
                    list.Add(new Vector2 { X = i, Y = k });
        return list;
    }

This should do it.

Edited, you can see the values like:

list[position].X
list[position].Y
Diogo
  • 176
  • 3