转换一个二维数组索引一维指数 [英] Convert a 2D array index into a 1D index

查看:444
本文介绍了转换一个二维数组索引一维指数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组的国际象棋变体,我在Java编码......我至今一个控制台版本,重新presents板作为一维数组(大小为32),但我的工作作出图形用户界面,我希望它显示为4×8格,所以我有JPanels ...

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it to appear as a 4x8 grid, so I have a 2-dimensional array of JPanels...

问题是,有没有可以在阵列转换任何式[I] [J]索引到数组[我]鉴于它是一个4×8阵列?

Question is, is there any formula that can convert the array[i][j] index into array[i] given the fact its a 4x8 array?

推荐答案

由于4列8行,那么:

i = row * 4 + col

编辑:我不好,没人抓我的这个错误很明显。但它实际上应该是行* 4 + COL

行* 8 + COL 将留在可能的不必要的索引差距。

row * 8 + col would leave unnecessary gaps in the possible indexes.

这篇关于转换一个二维数组索引一维指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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