问题描述
我正在尝试构建一个简单的应用程序,完成的程序看起来像这样:
梯子like-like-like-like-like-like-like-like-like-like-like-like-like-like-like-like游戏/lab9a.jpg
我还必须为此实施两个不同的GUI布局.现在,我试图找出执行此任务的最佳方法.我的教授告诉我,将元素类介绍4个州:
- 空
- 隐形(用于Gridlayout)
- 首字母
- 其他字母
我已经考虑过遵循解决方案(按列表,我的意思是任何类型的集合):
1.元素是一个字母,每行都是元素[].游戏类将是数组元素的数组[].我想那是最愚蠢的方法,验证可能很麻烦.
2.像以前一样,但行是元素的列表.游戏是一系列线条.
3.像以前一样,但游戏是行列表.
我应该选择哪一个?还是您有更好的想法?如果使用它,最好最好的收藏品?
推荐答案
您的网格是您的内部数据模型(即除了您使用它,没有一个).这就是为什么您可以选择最令人信服的一个.
我更喜欢第一个带有数组的解决方案,因为代码会更可读取(至少对我来说).只是比较:
grid[3][4] = element;
和
grid.get(3).add(4, element);
此外,如果要使用集合,则可能需要使用
Map<Integer, List<Element>> grid
问题描述
I'm trying to build a simple application, with the finished program looking like this :
ladder-like game http://img199.imageshack.us/img199/6859/lab9a.jpg
I will also have to implement two different GUI layouts for this. Now I'm trying to figure out the best method to perform this task. My professor told me to introduce Element class with 4 states :
- empty
- invisible (used in GridLayout)
- first letter
- other letter
I've thought about following solutions (by List I mean any sort of Collection) :
1. Element is a single letter, and each line is Element[]. Game class will be array of arrays Element[]. I guess that's the dumbest way, and the validation might be troublesome.
2. Like previously but Line is a List of Element. Game is an array of Lines.
3. Like previously but Game is a List of Lines.
Which one should I choose ? Or maybe do you have better ideas ? What collection would be best if to use one ?
推荐答案
Your grid is your internal data model (i.e. none except for you will use it). That's why you can choose the one which is the most convinient for you.
I would prefer the first solution with arrays because the code will be a little more readable (at least for me). Just compare:
grid[3][4] = element;
and
grid.get(3).add(4, element);
Moreover, if you want to use collections, then you probably need to use
Map<Integer, List<Element>> grid
where Integer-key represents row index. With list of lists it's very difficult to insert new words (just think, how would you implement that with lists only).