Quick question. I am currently making a synthetic division calculator and as a part of that I need to test whether or not a P/Q (a double) will work as a factor in an equation (given to the method as a list of integers).
Here is my code in the main class:
public static void main(String[] args)
{
List<Integer> coeffs = new ArrayList<>();
coeffs.add(1);
coeffs.add(-6);
coeffs.add(11);
coeffs.add(-6);
System.out.println(coeffs); // Returns [1, -6, 11, -6]
System.out.println(doesPqWork(coeffs, 1)); // Returns 'true' as intended
System.out.println(coeffs); // Returns [-6, 11, -6]
System.out.println(doesPqWork(coeffs, -2)); // Returns 'false' not as intended
System.out.println(coeffs); // Returns [11, -6]
System.out.println(doesPqWork(coeffs, 3)); // Returns 'false' not as intended
System.out.println(coeffs); // Returns [-6]
}
and here is the P/Q method itself:
private static boolean doesPqWork(List<Integer> coefficients, double PQ)
{
List<Double> results = new ArrayList<>();
double lastResult = coefficients.get(0);
coefficients.remove(0);
for(int coeff : coefficients) // 2, 1
{
double multiplied = PQ * lastResult;
lastResult = coeff + multiplied;
results.add(lastResult);
}
return results.get(results.size() - 1) == 0;
}
Please ignore how this method works to calculate it, I am only wondering why on earth the "coeffs" ArrayList is shrinking every time. I have been coding in Java for two years now and I have never come across this issue.
I thought that it was because when I call "coefficients.remove(0)" it is for some reason linking back to the "coeffs" list in the "main" method, yet when I create a new variable which would not possibly link back it still does weird thing.
I know a way I could fix this by creating some choppy code and repeating variables, but that seems really wasteful and I really want to know the source of this unintended behavior so I can fix it.
I am probably making a stupid mistake here, but if I am not then could someone please explain to me just what the heck is going on? Haha.