placedShapeInfo()
给了我几乎正确的结果,除了它给出了Shapes at (?,?)
的错误坐标,如下面的测试用例:
在placedShapeInfo()
中读取这一行时,似乎我做错了。
"("+coords.get(i)+"," + coords.get(i+1)+")"
每次将形状传递给placeShapeAt(int row, int col, Shape shape)
时,我会将row
和col
添加到Arraylist coords
。
smb可以帮我解决这个问题吗?
import java.util.*;
public class CreateSpace implements Space{
private int height;
private int width;
//private int placedAtRow = 0;
//private int placedAtCol = 0;
private String layout;
private char[][] space;
private Shape originalShape;
private ArrayList<CreateShape> shapes = new ArrayList<>();
private ArrayList<Integer> coords = new ArrayList<>();
Set<Character> shapesCount;
public CreateSpace(int height, int width, char[][] space, String layout)
{
this.height = height;
this.width = width;
this.space = space;
this.layout = layout;
}
public void placeShapeAt(int row, int col, Shape shape)
{
int row1 = row;
int col1 = col;
int sHeight = shape.getHeight();
int sWidth = shape.getWidth();
char [][] spaceWithShapes = space;
if(shapeFitsAt(row, col, shape))
{
for(int r = 0; r < sHeight; r++)
{
for(int c = 0; c < sWidth; c++)
{
if(spaceWithShapes[r+row][c+col] == '.' && shape.isFilledAt(r, c))// && shape.isFilledAt(r,c) == true) //|| (((CreateShape)shape).getShape()[r][c] == '.'))
{
spaceWithShapes[r+row][c+col] = shape.getDisplayChar();
}
}
}
shapes.add((CreateShape)shape);
coords.add((Integer)row1);
coords.add((Integer)col1);
//shapes.add((Integer)row1);
Collections.sort(shapes);
setSpace(spaceWithShapes);
}
else
throw new FitItException("The shape does not fit!");
}
public String placedShapeInfo()
{
Collections.sort(shapes);
int shapesTotal = placedShapeCount();
String desc = shapesTotal + " shapes placed";
String getShapeInfo = "";
for(int i = 0; i < shapes.size(); i++)
//for(int j = 0; j < coords.size(); j++)
{
getShapeInfo +="Shape at " + "("+coords.get(i)+"," + coords.get(i+1)+")" + "\n" + shapes.get(i).toString() + "\n";
System.out.println();
}
return desc + "\n" +
getShapeInfo;
答案 0 :(得分:2)
您的coords
是整数列表。所以你在(1,2)和(3,4)处添加了两个形状。所以coords
现在是(1,2,3,4)。
然后,您遍历形状列表,并为每个形状获取coords.get(i)
和coords.get(i+1)
。你看到了问题吗?
对于你的第一个形状,i = 0,你得到coords
0和1,它给你(1,2)。
对于你的第二个形状,i = 1,你得到coords
1和2,它给你(2,3)。
您一直没有使用i
。也许每个形状都应该知道它自己的坐标,你应该问它的x和y而不是将它们存储在外部数据结构中。这是处理它的一种方式,这就是OO编程擅长的事情。