Array with negative indexes

前端 未结 5 1512
时光说笑
时光说笑 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: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

提交回复
热议问题