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

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

问题描述

我有两个用于国际象棋变体的数组,我正在用 java 编码......到目前为止,我有一个控制台版本,它将棋盘表示为一维数组(大小为 32),但我正在为它制作一个 GUI 和我希望它显示为 4x8 网格,所以我有一个二维的 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...

问题是,考虑到它是一个 4x8 数组,是否有任何公式可以将数组 [i][j] 索引转换为数组 [i]?

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 列 x 8 行:

Given 4 columns by 8 rows then:

i = row * 4 + col

我的错,显然没有人发现我犯了这个错误.但它实际上应该是 row * 4 + col.

My bad, nobody caught me on this mistake apparently. But it should actually be row * 4 + col.

row * 8 + col 会在可能的索引中留下不必要的间隙.

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

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

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