我认为没有理由不应该这样做。好的,这里有几个问题:
positionList = new ArrayList<>();
不会编译。肯定是positionList = new ArrayList<Position>();
- 正如 Jodaka 所说,当您第二次获得每个位置时,您将在列表中获得两次(当您将其称为第四次时会获得三次,依此类推)。
- 您的实现将包括第一列和第一行,但不包括最后一列(尝试
i <= maxColumn
并j <= maxRow
在循环中)。
对于我的测试,我使用 Points 而不是 Position,并为 Points 添加了一些强制转换,使用 int 作为构造函数参数,但返回 double。但我并没有改变逻辑本身:
package spreadsheet;
import java.awt.Point;
import java.util.ArrayList;
public class Range {
private final Point a;
private final Point b;
private final ArrayList<Point> PointList;
// Creates a new range, where it makes sure that the Points,
// appear in the right order, where the first Point is the Point
// of the upper left corner, and the second Point is the lower right corner.
public Range(final Point a, final Point b) {
int minColumn = (int) Math.min(a.getX(),b.getX());
int minRow = (int) Math.min(a.getY(),b.getY());
int maxColumn =(int) Math.max(a.getX(),b.getX());
int maxRow = (int) Math.max(a.getY(),b.getY());
this.a = new Point(minColumn, minRow);
this.b = new Point(maxColumn, maxRow);
PointList = new ArrayList<Point>();
}
public ArrayList<Point> getPoints() {
int minColumn = (int) a.getX();
int minRow = (int) a.getY();
int maxColumn = (int) b.getX();
int maxRow = (int) b.getY();
for (int i = minColumn; i < maxColumn; i++) {
for (int j = minRow; j < maxRow; j++) {
PointList.add(new Point(i, j));
}
}
return PointList;
}
考试:
package spreadsheet;
import java.awt.Point;
import java.util.ArrayList;
import org.junit.Test;
public class RangeTest {
@Test
public void testSomePoints() throws Exception {
Range range = new Range(new Point(1, 1), new Point(5, 5));
ArrayList<Point> points = range.getPoints();
for (Point point : points) {
System.out.println(point);
}
}
}
结果:
java.awt.Point[x=1,y=1]
java.awt.Point[x=1,y=2]
java.awt.Point[x=1,y=3]
java.awt.Point[x=1,y=4]
java.awt.Point[x=2,y=1]
java.awt.Point[x=2,y=2]
java.awt.Point[x=2,y=3]
java.awt.Point[x=2,y=4]
java.awt.Point[x=3,y=1]
java.awt.Point[x=3,y=2]
java.awt.Point[x=3,y=3]
java.awt.Point[x=3,y=4]
java.awt.Point[x=4,y=1]
java.awt.Point[x=4,y=2]
java.awt.Point[x=4,y=3]
java.awt.Point[x=4,y=4]
编辑:
如果我第二次调用 getPoints(),我也会看到每个点两次。您创建一次列表,但每次调用 getPoints() 时都会添加点。
有几种可能性:
- 您可以在 getPoints() 方法中而不是在构造函数中创建 ArrayList。但通常你不想每次调用这个方法时都创建一个全新的列表......
- 您可以将列表存储在字段中,在构造函数中计算它并仅使用返回该字段的 getter。
- 类似于第二种可能性,但您可以按需计算列表。所以不要在构造函数中计算它,而是在 getter 中计算它,如果它现在为空,否则使用存储的列表。
2. 编辑
选项:
public class Range {
private final Point a;
private final Point b;
private final ArrayList<Point> PointList;
public Range(final Point a, final Point b) {
int minColumn = (int) Math.min(a.getX(),b.getX());
int minRow = (int) Math.min(a.getY(),b.getY());
int maxColumn =(int) Math.max(a.getX(),b.getX());
int maxRow = (int) Math.max(a.getY(),b.getY());
this.a = new Point(minColumn, minRow);
this.b = new Point(maxColumn, maxRow);
}
public ArrayList<Point> getPoints() {
PointList = new ArrayList<Point>();
int minColumn = (int) a.getX();
int minRow = (int) a.getY();
int maxColumn = (int) b.getX();
int maxRow = (int) b.getY();
for (int i = minColumn; i < maxColumn; i++) {
for (int j = minRow; j < maxRow; j++) {
PointList.add(new Point(i, j));
}
}
return PointList;
}
}
选项:
public class Range {
private final Point a;
private final Point b;
private final ArrayList<Point> PointList;
public Range(final Point a, final Point b) {
int minColumn = (int) Math.min(a.getX(),b.getX());
int minRow = (int) Math.min(a.getY(),b.getY());
int maxColumn =(int) Math.max(a.getX(),b.getX());
int maxRow = (int) Math.max(a.getY(),b.getY());
this.a = new Point(minColumn, minRow);
this.b = new Point(maxColumn, maxRow);
PointList = calcPoints();
}
private ArrayList<Point> calcPoints() {
ArrayList<Point> list = new ArrayList<Point>();
int minColumn = (int) a.getX();
int minRow = (int) a.getY();
int maxColumn = (int) b.getX();
int maxRow = (int) b.getY();
for (int i = minColumn; i < maxColumn; i++) {
for (int j = minRow; j < maxRow; j++) {
PointList.add(new Point(i, j));
}
}
return list;
}
public ArrayList<Point> getPoints() {
return PointList;
}
}
选项:
public class Range {
private final Point a;
private final Point b;
private final ArrayList<Point> PointList;
public Range(final Point a, final Point b) {
int minColumn = (int) Math.min(a.getX(),b.getX());
int minRow = (int) Math.min(a.getY(),b.getY());
int maxColumn =(int) Math.max(a.getX(),b.getX());
int maxRow = (int) Math.max(a.getY(),b.getY());
this.a = new Point(minColumn, minRow);
this.b = new Point(maxColumn, maxRow);
}
private ArrayList<Point> calcPoints() {
ArrayList<Point> list = new ArrayList<Point>();
int minColumn = (int) a.getX();
int minRow = (int) a.getY();
int maxColumn = (int) b.getX();
int maxRow = (int) b.getY();
for (int i = minColumn; i < maxColumn; i++) {
for (int j = minRow; j < maxRow; j++) {
list.add(new Point(i, j));
}
}
return list;
}
public ArrayList<Point> getPoints() {
if(PointList == null) {
PointList = calcPoints();
}
return PointList;
}
}