Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see no reason why that shouldn't work. Ok, there are a few problems here:</p> <ul> <li><code>positionList = new ArrayList&lt;&gt;();</code> will not compile. It must be <code>positionList = new ArrayList&lt;Position&gt;();</code></li> <li>As Jodaka said you would get every Position twice in the list when you get it the second time (three times when you call it the fourth and so on).</li> <li>Your implementation would include the first column and row but not the last (try <code>i &lt;= maxColumn</code> and <code>j &lt;= maxRow</code> in the loops).</li> </ul> <p>For my test I used Points instead of Position and added some casts for Points use ints as constructor argument but return double. But I didn't change the logic itself:</p> <pre><code>package spreadsheet; import java.awt.Point; import java.util.ArrayList; public class Range { private final Point a; private final Point b; private final ArrayList&lt;Point&gt; 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&lt;Point&gt;(); } public ArrayList&lt;Point&gt; 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 &lt; maxColumn; i++) { for (int j = minRow; j &lt; maxRow; j++) { PointList.add(new Point(i, j)); } } return PointList; } </code></pre> <p>The Test:</p> <pre><code>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&lt;Point&gt; points = range.getPoints(); for (Point point : points) { System.out.println(point); } } } </code></pre> <p>The result:</p> <pre><code>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] </code></pre> <p><em>edit:</em></p> <p>I would see every point twice too, if I would call getPoints() a second time. You create the list once but add points every time you call getPoints().</p> <p>There are several possibilities:</p> <ol> <li>You could create the ArrayList in the getPoints() method and not in the constructor. But normally you wouldn't want to create an entirely new list each time you call this method...</li> <li>You could store the list in a field, calculate it in the constructor and use only a getter that returns the field.</li> <li>Similar to the second possibility, but you could calculate the list on demand. So don't calculate it in the constructor but in the getter <strong>if it is null by now</strong> and use the stored list otherwise.</li> </ol> <p><em>2. edit</em></p> <ol> <li><p>option:</p> <pre><code>public class Range { private final Point a; private final Point b; private final ArrayList&lt;Point&gt; 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&lt;Point&gt; getPoints() { PointList = new ArrayList&lt;Point&gt;(); 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 &lt; maxColumn; i++) { for (int j = minRow; j &lt; maxRow; j++) { PointList.add(new Point(i, j)); } } return PointList; } } </code></pre></li> <li><p>option:</p> <pre><code>public class Range { private final Point a; private final Point b; private final ArrayList&lt;Point&gt; 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&lt;Point&gt; calcPoints() { ArrayList&lt;Point&gt; list = new ArrayList&lt;Point&gt;(); 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 &lt; maxColumn; i++) { for (int j = minRow; j &lt; maxRow; j++) { PointList.add(new Point(i, j)); } } return list; } public ArrayList&lt;Point&gt; getPoints() { return PointList; } } </code></pre></li> <li><p>option:</p> <pre><code>public class Range { private final Point a; private final Point b; private final ArrayList&lt;Point&gt; 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&lt;Point&gt; calcPoints() { ArrayList&lt;Point&gt; list = new ArrayList&lt;Point&gt;(); 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 &lt; maxColumn; i++) { for (int j = minRow; j &lt; maxRow; j++) { list.add(new Point(i, j)); } } return list; } public ArrayList&lt;Point&gt; getPoints() { if(PointList == null) { PointList = calcPoints(); } return PointList; } } </code></pre></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload