Is there a Java function that can take as parameters two Arrays and multiply every element in the 1st array with the equivalent in the second array?
If there is one. How is that function more efficient in comparison to a for loop
?
Array 1
1, 2, 3, 4
Array 2
4, 3, 2, 1
Result:
4, 6, 6, 4
No built in function I know of, but very simple to achieve:
int[] arr = {1, 2, 3, 4};
int[] arr2 = {4, 3, 2, 1};
int[] result = new int[4];
for(int i=0 ; i<arr.length ; i++) {
result[i] = arr[i]*arr2[i];
}
You could potentially make the operation multi-threaded (quite easily) if you really need better performance, but aside from that I don't see a magic, huge performance boost. It's quite a cheap operation really.