一维数组作为多维数组的等价物? [英] Single-dimensional arrays as equivalents of multi-dimensional arrays?

查看:119
本文介绍了一维数组作为多维数组的等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组定义如下:

I have an array defined as follows:

Tile[,] Tiles

它包含在X轴100个元素,且在Y轴150的元件。这不同于地图在我的游戏地图,虽然,但我总是知道瓷砖在X和金额Y轴。

It contains 100 elements in the X axis, and 150 elements in the Y axis. This differs from map to map in my game though, but I always know the amount of tiles in the X and the Y axis.

,我怎么可以在上述数组转换成一维数组,遍历它们?我知道,一维数组比多维数组快2倍,这意味着很多我的比赛表现。此外,单维数组可以并行计算中使用。

Given this, how can I convert the above array to a single-dimensional array, and iterate through them? I know that single-dimensional arrays are 2 times faster than multi-dimensional arrays, and that means a lot to the performance of my game. Furthermore, single dimensional arrays can be used with parallel computing.

推荐答案

好吧,如果项目在Y轴数是固定的,每X,你可以试试这个。结果
假设A =的指望X轴的和b =的 Y轴计数的;结果
你可以定义瓷砖=新瓦[A * B]; 结果
然后瓷砖[X,Y] 变成瓷砖[X * B + Y]

Well, if items count on Y axis is fixed for every X, you can try this.
Assume a = count on X axis and b = count on Y axis;
you can define Tiles = new Tile[a*b];
Then Tiles[x,y] becomes Tiles[x*b+y]

这篇关于一维数组作为多维数组的等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆