Click here to Skip to main content
15,886,830 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been looking for an example on the web and don't seem to see anything like what I'm trying to do.

I have a 2 dimensional integer array, where the first number is a sequential number starting at zero and going to 20 (or less) and the second is a 4 digit integer that would seem to be random but is repeatable having up to 120 data points each max.

I'm trying to build sequential iterations of data using a single dimensional array (720 elements) with zeros and duplicates, that I will then remove duplicates and zeros from and any occurrences of identical numbers from the two dimensional array and then add the cleaned up array to the next sequential number of the two dimensional array. The final array will have distinct data points. (I have a large list class that I'm getting the data points from.)

Being able to take one of these single arrays out of the multidimensional array might also be useful. I'm using Linq, and thought I saw this sort of example before, but I just can't seem to find it now. Any suggestions?
Posted

All such problems are solved in a really simple way, but there are some simple but not so obvious techniques.

Here is the wrapper class which behaves like a 2D and 1D array at the same time, based on the same physical locations of array elements. The under-the-hood implementation array is the 1D one:
C#
interface IArray2D<T> {
    T this[int x, int y] { get; set; }
} //Array2D

public class DualUseArray<T> : IArray2D<T> {

    public DualUseArray(int sizeX, int sizeY ) {
        this.implementation = new T[sizeX * sizeY];
        this.width = sizeX;
    } //DualUseArray

    public T this[int x, int y] {
        get { return implementation[GetIndex(x, y)]; }
        set { implementation[GetIndex(x, y)] = value; }
    } //this

    int GetIndex(int x, int y) {
        return y * width + x;
    } //GetIndex

    public T this[int index] {
        get { return implementation[index]; }
        set { implementation[index] = value; }
    } //this

    T[] implementation;
    int width;

} //DualUseArray


Here is the double-use way:
C#
DualUseArray<string> myArray = new DualUseArray<string>(12, 100);
myArray[120] = "120";
myArray[12, 10] = "12, 10";
string value = myArray[120];


Got the idea?

Pay attention for the auxiliary interface used. It looks like this is the only way to introduce more than one indexed property "this" in the same type. Of course all indexed properties should be different in their signatures.

You can improve this solution in some ways: you can check up the range constraints for the 2D indices, as well as check up validity of constructor parameters, you can add constructors initializing the array elements to same value or some other ways, maybe something else. In the same way, you can introduce representations like 3D, 4D, etc...

—SA
 
Share this answer
 
v4
Comments
Maciej Los 26-Jul-13 18:37pm    
Great 5!
Sergey Alexandrovich Kryukov 26-Jul-13 18:43pm    
Thank you, Maciej.
—SA
Maciej Los 26-Jul-13 18:48pm    
No, no... We all thank you for your willingness to share your knowledge ;)
Sergey Alexandrovich Kryukov 26-Jul-13 21:38pm    
:-)
If i recognize your problem correctly then possibly you are looking for a way to split your mufti-dimensional array to single arrays.Is it?
Then these 2 will definitely help you..
http://stackoverflow.com/questions/4801990/how-to-get-a-dimension-slice-from-a-multidimensional-array[^]
http://stackoverflow.com/questions/9322479/converting-two-dimensional-array-to-single-dimensional-in-c[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900