java - 如何将两个具有相同长度的数组相乘并返回具有这些值的新数组?
问题描述
public static double multi(double u[][]) {
double x[] = { 1, 2, 3 };
double y[] = { 4, 5, 6 };
for (int i = 0; i < x.length; i++) {
for (double j = 0; i < y.length; j++) {
double z = x[i] * y[i];
return z;
}
}
return 0;
}
到目前为止,这是我的代码。例如,我想与 乘以arr1[] = {1,2,3};
并arr2 [] = {4,5,6};
返回相同的长度,乘以乘以arr1
和的值arr2
。[4,10,18]
像这样:1*4
、2*5
和3*6
。
还有一点很重要:任务是将它返回到一个新数组中。
解决方案
只需声明一个z
与其他两个数组大小相同的新数组。遍历它们,将每个索引值相乘并返回数组z
;
public static double[] multi() {
double x[] = { 1, 2, 3 };
double y[] = { 4, 5, 6 };
double z[] = new double[3];
for (int i = 0; i < x.length; i++) {
z[i] = x[i] * y[i];
}
return z;
}
推荐阅读
- sql - 我可以从 db 返回行结果而不先在 Golang func 中扫描它吗?
- javascript - Guild.commands 在 discord.js v12.5.3 中未定义
- javascript - 有没有办法从生成的条形码创建图像或矢量文件?
- sql - 连接和/或子查询或排名函数
- javascript - 在浏览器中扩展动态加载的模块类
- c++ - 使用队列 cpp 的多线程请求
- rascal - 在 eval 中使用导入的代数数据类型
- ios - Swift:如何在不同的表视图中加载 JSON
- html - 网站底部出现黑框
- vue.js - 无法在 vue JS 应用程序中将 process.env 链接到 Heroku 配置变量