I would appreciate the communities support on how to multiply an array labelled as double
by an array labelled int
, which returns a array similar to the logic below, then stores the value?
[1, 2][1, 2, 3]
1*1 2*1
1*2 2*2
1*3 2*3
[1, 2, 3][2, 4, 6]
I have tried:
import java.util.Arrays;
public class exercise_3_3 {
public static void main(String[] args) {
double[] conversion = new double[] {0.62, 0.54};
int[] distances = new int[] {2, 5, 10};
double kilometers = (double)(conversion * distances);
System.out.println(Arrays.toString(kilometers));
}
}
However, I get the following error:
The operator * is undefined for the argument type(s) double[], int[]
I can imagine this is possible with a for-loop
, however, I have not reached this stage yet on the book I'm studying from. Though, I would really appreciate an answer without a for-loop
and one with the use of a for-loop
.
Using the recommended reference, I have come up with this, although It does not work effectively. I would really appreciate some feedback towards my solution:
import java.util.Arrays;
public class testLoop {
static double b[] = {0.62, 0.54};
static int a[] = {2, 5, 10};
public static void mul() {
double c[] = new double[1];
for(double i=0; i<b.length;i++) {
for(int j=0;j<a.length;j++) {
c[j] = 0;
System.out.println(Arrays.toString(c));
}
}
}
}
Also, could someone explain to me whether I require a multidimensional array for multiplying a double
vector with an int
vector, and why this is necessary?