I want to multiply two arrays in java. Is there any function for that or how can I do that?
int[] a = new int[]{1,2,3};
int[] b = new int[]{1,2,3};
int[] c = a * b; ?
I researched a bit but could not find.
There is no built in functionality for this. However, it can be done manually, using streams, libraries, or a number of other approaches.
The easiest and most straightforward approach is to use a for loop.
static int[] arrayMultiply(int[] a, int[] b) {
int newLength = Math.min(a.length, b.length);
int[] c = new int[newLength];
for (int index = 0; index < newLength; index++) {
c[index] = a[index] * b[index];
}
return c;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments