Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying Array, Allow it to remain the same when input to constructor changes
    text
    copied!<p>My point class is immutable. When I am given input into the constructor originally, it should be copied into the cloneList object. This would allow for it to remain the way it was before if a couple of indexes in the array are changed via the constructor. I have tried almost every possible combination and am still running into the trouble. I want the cloneList to be a copy of the original Point[] points array, thus if the points array is changed then the cloneList will not. </p> <pre><code>import java.util.Arrays; import java.util.Iterator; import java.util.List; public class Polygon { private double xSum = 0; private double ySum = 0; private Point[] points; private Point[] cloneList; private Point a; public Polygon(Point[] points) { this.points = points; cloneList = new Point[points.length]; for (int i = 0; i &lt; cloneList.length; i++) { cloneList[i] = points[i]; } for (int i = 0; i &lt; cloneList.length; i++) System.out.println(cloneList[i]); for (int i = 0; i &lt; points.length; i++){ cloneList[i] = points[i]; // System.out.print(cloneList[i].getX()); // System.out.print(cloneList[i].getY()); // System.out.println(); } public Point getVertexAverage() { double xSum = 0; double ySum = 0; for (int index = 0; index &lt; cloneList.length; index++) { xSum = xSum + cloneList[index].getX(); ySum = ySum + cloneList[index].getY(); } return new Point(xSum / getNumSides(), ySum / getNumSides()); } public int getNumberSides() { return cloneList.length; } </code></pre> <p>}</p>
 

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