.toEquals(Object o)Add a copy method to our ArrayCalculator class
What is the return value? What arguments do we need?
ArrayCalculator calcOne = new ArrayCalculator(new double[]{1, 2, 3, 4, 5});
ArrayCalculator calcFour = calcOne.copy();
System.out.println(calcFour); // 1.0 2.0 3.0 4.0 5.0
System.out.println(calcFour.equals(calcOne)); // true
calcFour.setNumber(0, 99);
System.out.println(calcFour.equals(calcOne)); // false.copy() solutionUse instance method .clone() to make a shallow copy of the entire array:
Use Arrays class method .copyOf(Type[] originalArray, int length) to copy all or some elements of the original array.
import java.util.Arrays;
double[] numbers = new double[]{3.4, 5.5, 7.6, 10.0};
double[] otherNumbers = Arrays.copyOf(numbers, 4);Use Arrays class method .copyOfRange(Type[] originalArray, int from, int to) to copy all or some elements of the original array.
Use System class method .arraycopy(Type[] originalArray, int indexOriginal, Type[] destinationArray, int indexDestination) to copy over elements from one array to another:
array.clone() instance method, creates a new array that is an exact shallow copy of the original with the same length and typeArrays.copyOf(...) and Arrays.copyOfRange(...) class (static) Arrays methods, create a new array of a specified length and copies elements into itSystem.arraycopy(...) class (static) System method, copies a specified range of elements into an existing destination arrayWhy does
.clone()take no arguments?
clone() method takes no arguments (typical situation)clone() method take one argument? (hint: think static method)