If I have an array int[] a = new int[]{1, 2, 3};
and another int[] b = new int[]{3, 2};
and I want to add the two together, I would do:
if (a.length >= b.length){
int[] c = new int[a.length];
for(int i=0; i<c.length; i++){
c[i] = a[i] + b[i];
return c;
}
}
else{
int[]c = new int[b.length];
for(int i=0; i<c.length; i++){
c[i] = a[i] + b[i];
return c;
}
But when I print c, I get {4, 4} and the 3 on the end is left out, where am I going wrong?
Thanks in advance for any help!
public Poly add(Poly a){
if (coefficients.length <= a.coefficients.length){
int[] c = new int[coefficients.length];
for (int i=0; i<added.length; i++){
c[i] = a.coefficients[i] + coefficients[i];
}
Poly total = new Poly(c);
return total;
}
else{
int[] added = new int[a.coefficients.length];
for (int i=0; i<added.length; i++){
added[i] = a.coefficients[i] + coefficients[i];
}
Poly total = new Poly(c);
return total;
}
}
and Poly is a constructor that takes an int array as an argument (Poly ex = new Poly(new int[]{1, 2, 3})
)