0

I have an assignment where I am working on a Spreadsheet, that I have been handed by my instructor.

I am being asked to implement a class called Range, that accepts two positions (each consisting of two ints denoting column and row, for instance new Position(1,2)), and then finds the lowest row and column and constructs a new position which these two values, this position is the upper left corner of my range, and the same with the highest value for row and column.

Then I was asked to make a class that takes the sum of a range of positions. So I decided that for my range it should be able to have a method where all the positions where put into an ArrayList, that is the getPositions() method.

Here you can see the source code of the class:

package spreadsheet;

import java.lang.Math;
import java.util.ArrayList;

public class Range {

    private Position a;
    private Position b;
    private ArrayList<Position> positionList;

    // Creates a new range, where it makes sure that the positions,
    // appear in the right order, where the first position is the position
    // of the upper left corner, and the second position is the lower right corner.
    public Range(final Position a, final Position b) {
        int minColumn = Math.min(a.getColumn(),b.getColumn());
        int minRow = Math.min(a.getRow(),b.getRow());

        int maxColumn = Math.max(a.getColumn(),b.getColumn());
        int maxRow = Math.max(a.getRow(),b.getRow());

        this.a = new Position(minColumn, minRow);
        this.b = new Position(maxColumn, maxRow);
        positionList = new ArrayList<>();       
    }


    public ArrayList<Position> getPositions() {
        int minColumn = this.a.getColumn();
        int minRow = this.a.getRow();
        int maxColumn = this.b.getColumn();
        int maxRow = this.b.getRow();
        for(int i = minColumn; i < maxColumn; i++) {
            for(int j = minRow; j < maxRow; j++) {
                positionList.add(new Position(i, j));
            }
        }
        return positionList;
    }   
}

The problem is however that it does not really work, the list it returns is empty, so why is that? Can anyone spot the error?

4

1 回答 1

1

我认为没有理由不应该这样做。好的,这里有几个问题:

  • positionList = new ArrayList<>();不会编译。肯定是positionList = new ArrayList<Position>();
  • 正如 Jodaka 所说,当您第二次获得每个位置时,您将在列表中获得两次(当您将其称为第四次时会获得三次,依此类推)。
  • 您的实现将包括第一列和第一行,但不包括最后一列(尝试i <= maxColumnj <= 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() 时都会添加点。

有几种可能性:

  1. 您可以在 getPoints() 方法中而不是在构造函数中创建 ArrayList。但通常你不想每次调用这个方法时都创建一个全新的列表......
  2. 您可以将列表存储在字段中,在构造函数中计算它并仅使用返回该字段的 getter。
  3. 类似于第二种可能性,但您可以按需计算列表。所以不要在构造函数中计算它,而是在 getter 中计算它,如果它现在为空,否则使用存储的列表。

2. 编辑

  1. 选项:

    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;
        }
    }
    
  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);
            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;
        }
    }
    
  3. 选项:

    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;
        }
    }
    
于 2013-01-23T08:22:19.167 回答