Array with negative indexes

前端 未结 5 1508
时光说笑
时光说笑 2020-12-11 08:49

I have an array which I\'m using to store map data for a game I\'m working on.

MyMapType[,,] map; 

The reason I\'m using a fixed array inst

相关标签:
5条回答
  • 2020-12-11 09:04

    Use the Dictionary class, since you can assign whatever values you want for either the key or value. While I'm not sure how this would work for the 3-dimensional array that you showed above, I can show how this would work if this were a 1-dimensional array, and you can infer how to best make use of it:

    MyMapType[] map;
    
    //map is filled with w/e data
    
    Dictionary<int, MyMapType> x = new Dictionary<int, MyMapType>();
    
    x[-1] = //(map data for whatever value is for the negative value);
    x[0] = map[0]
    //(etc...)
    
    0 讨论(0)
  • 2020-12-11 09:07

    I'd like to note here that dictionaries allow for negative indexes and a 2D dictionairy can solve problems like these too, just think about the datastructure and if you can live with a dictionary

    note that dictionaries and lists are used in different scenario's. and their speed depends on what functions are used on them

    0 讨论(0)
  • 2020-12-11 09:15

    If you want "negative" indexes C# 8 now supports it.

    var words = new string[]
    {
                    // index from start    index from end
        "The",      // 0                   ^9
        "quick",    // 1                   ^8
        "brown",    // 2                   ^7
        "fox",      // 3                   ^6
        "jumped",   // 4                   ^5
        "over",     // 5                   ^4
        "the",      // 6                   ^3
        "lazy",     // 7                   ^2
        "dog"       // 8                   ^1
    };              // 9 (or words.Length) ^0
    

    So the to call the negative one would be like this

    words[^1]
    

    See this link

    So in your case the middle element could be the zero Z

    0 讨论(0)
  • 2020-12-11 09:20

    Could you try to store a list of MyMapTime[,] in two lists:

    • one for z values of greater than or equal to 0
    • and second of negative z-values.

    The index of the tables would be the value of z. Having this would let you access quickly the xy-values for specific z-level. Of course the question is: what are your z-values? Are there sparse or dense. Even for sparse values you would end up with an array holding null values for [,].

    0 讨论(0)
  • 2020-12-11 09:23

    replace your arrays with a class:

    class MyArray {
        private MyMapType[] myArray = new myMapType[size]
        MyMapType this[index] {
           get{return myArray[index + offset];}
        }
    
    }
    

    you can set the size and the offset in the constructor or even change it at will.

    Building on this example here is another version:

    class MyArray {
        private MyMapType[] positives = new myMapType[size]
        private MyMapType[] negatives = new myMapType[size-1]
        MyMapType this[index] {
           get{return index >= 0 ? positives[index] : negateves[1-index];}
        }
    
    }
    

    It does not change the fact that you need to set the size for both of them. Honestly I like the first one better

    0 讨论(0)
提交回复
热议问题