在类似炸弹人的益智游戏中,这些线条是为了确定 throw 炸弹的最佳位置(两个 crate 之间)。这是一个 13*11 的网格。
当我检查 2 个物体的大小时,存放 X 的那个比另一个大得多。 它们应该具有相同的尺寸,每个坐标 X 都带有他的 Y。
由于第一个最佳移动位于同一 Y 轴上,我怀疑第二个 arraylist 不会创建一个新房间来存储它们(但它是 ArrayList 而不是如此设置?)。
ArrayList<Integer> bestSpotX=new ArrayList<Integer>();
ArrayList<Integer> bestSpotY=new ArrayList<Integer>();
// ...
for (int line=0, col=0;line<11 && col<13;line++, col++) {
if ((map[line].charAt(col)=='0')&&(map[line+2].charAt(col)=='0')) {
bestSpotX.add(col);
bestSpotY.add(line+1);
}
}
我的迭代是错误的,还是我添加坐标值的方式?
最佳答案
为什么:
ArrayList<Integer> bestSpotX=new ArrayList<Integer>();
ArrayList<Integer> bestSpotY=new ArrayList<Integer>();
而不是:
class LineColLocation {
protected int line, col;
// constructors, setters and getters
}
ArrayList<LineColLocation> bestSpots=new ArrayList<LineColLocation>();
int bestSpotLine=...; // ... whatever logic to ...
int bestSpotCol=...; // determine a *best spot*
LineColLocation bestSpot=new LineColLocation(bestSpotLine, bestSpotCol);
bestSpots.add(bestSpot);
这样您就不会意外地弄乱两个不同列表中相同数量的 X 和 Y
的**隐式要求* - 您只需具体化此约束(使其显式化),无需对此约束进行验证,并且您每次都可以确保不会搞砸事情。
关于java - 坐标 ArrayList 应该具有相同的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39801995/