时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 将两个整数数组元素中的值相除
Java 将两个整数数组元素中的值相除
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
475
查看 / 运行 实例源码
将两个整数数组中的值逐元素相除。
实例源码:
源代码:
执行
执行中...
//package com.nowjava; public class Main {/*N o w J a v a . c o m 提 供*/ public static void main(String[] argv) throws Exception { int[] a = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; int[] b = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(java.util.Arrays .toString(divideElementwise(a, b))); } /** * Divides the values in the two arrays of integers element-wise. * * @param a Array of integer dividends. * @param b Array of integer divisors. * @return Result of element-wise division of arrays. * @throws ArithmeticException Throws exception when the arrays differ in * length. */ public static double[] divideElementwise(int[] a, int[] b) throws ArithmeticException { if (a.length != b.length) { throw new ArithmeticException(); } else { double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) {// 来自 NowJava.com - 时 代 Java result[i] = (double) a[i] / b[i]; } return result; } } /** * Divides the values in the two arrays of doubles element-wise. * * @param a Array of double dividends. * @param b Array of double divisors. * @return Result of element-wise division of arrays. * @throws ArithmeticException Throws exception when the arrays differ in * length. */ public static double[] divideElementwise(double[] a, double[] b) throws ArithmeticException { if (a.length != b.length) { throw new ArithmeticException(); } else { double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = a[i] / b[i]; } return result; } } /** * Divides the values in the two arrays of longs element-wise. * * @param a Array of long dividends. * @param b Array of long divisors. * @return Result of element-wise division of arrays. * @throws ArithmeticException Throws exception when the arrays differ in * length. */ public static double[] divideElementwise(long[] a, long[] b) throws ArithmeticException { if (a.length != b.length) { throw new ArithmeticException(); } else { double[] result = new double[a.length]; for (int i = 0; i < a.length; i++) { result[i] = (double) a[i] / b[i]; } return result; } } /** * Divides the values in the two arrays of floats element-wise. * * @param a Array of float dividends. * @param b Array of float divisors. * @return Result of element-wise division of arrays. * @throws ArithmeticException Throws exception when the arrays differ in * length. */ public static float[] divideElementwise(float[] a, float[] b) throws ArithmeticException { if (a.length != b.length) { throw new ArithmeticException(); } else { float[] result = new float[a.length]; for (int i = 0; i < a.length; i++) { result[i] = a[i] / b[i]; } return result; } } /** * Divides the values in the two arrays of bytes element-wise. * * @param a Array of byte dividends. * @param b Array of byte divisors. * @return Result of element-wise division of arrays. * @throws ArithmeticException Throws exception when the arrays differ in * length. */ public static float[] divideElementwise(byte[] a, byte[] b) throws ArithmeticException { if (a.length != b.length) { throw new ArithmeticException(); } else { float[] result = new float[a.length]; for (int i = 0; i < a.length; i++) { result[i] = (float) a[i] / b[i]; } return result; } } /** * Divides the values in the two matrices element-wise. * * @param a Matrix of double dividends. * @param b Matrix of double divisors. * @return Result of element-wise division of the two matrices. * @throws ArithmeticException Throws an exception when the two matrices are * not of the same size. */ public static double[][] divideElementwise(double[][] a, double[][] b) throws ArithmeticException { if (a.length != b.length || a[0].length != b[0].length) { throw new ArithmeticException(); } else { double[][] result = new double[a.length][a[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { result[i][j] = a[i][j] / b[i][j]; } } return result; } } /** * Divides the values in the two matrices element-wise. * * @param a Matrix of integer dividends. * @param b Matrix of integer divisors. * @return Result of element-wise division of the two matrices. * @throws ArithmeticException Throws an exception when the two matrices are * not of the same size. */ public static double[][] divideElementwise(int[][] a, int[][] b) throws ArithmeticException { if (a.length != b.length || a[0].length != b[0].length) { throw new ArithmeticException(); } else { double[][] result = new double[a.length][a[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { result[i][j] = (double) a[i][j] / b[i][j]; } } return result; } } /** * Divides the values in the two matrices element-wise. * * @param a Matrix of float dividends. * @param b Matrix of float divisors. * @return Result of element-wise division of the two matrices. * @throws ArithmeticException Throws an exception when the two matrices are * not of the same size. */ public static float[][] divideElementwise(float[][] a, float[][] b) throws ArithmeticException { if (a.length != b.length || a[0].length != b[0].length) { throw new ArithmeticException(); } else { float[][] result = new float[a.length][a[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { result[i][j] = a[i][j] / b[i][j]; } } return result; } } /** * Divides the values in the two matrices element-wise. * * @param a Matrix of double dividends. * @param b Matrix of double divisors. * @return Result of element-wise division of the two matrices. * @throws ArithmeticException Throws an exception when the two matrices are * not of the same size. */ public static double[][] divideElementwise(long[][] a, long[][] b) throws ArithmeticException { if (a.length != b.length || a[0].length != b[0].length) { throw new ArithmeticException(); } else { double[][] result = new double[a.length][a[0].length]; for (int i = 0; i < a.length; i++) { for (int j = 0; j < a[0].length; j++) { result[i][j] = (double) a[i][j] / b[i][j]; } } return result; } } /** * Divides the values in the two matrices element-wise. * * @param a Matrix of float dividends. * @param b Matrix of float divisors. * @return Result of element-wise division of the two matrices. * @throws ArithmeticException Throws an exception when the two matrices are * not of the same size. */ /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部