热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

两个数组对应数相减java_Java将两个数组相减(按组件)

来自nowjava.com–时代Javapackagecom.nowjava;publicclassMain{publicstaticvoidmain(String[]argv)t

// 来自 n o w j a v a . c o m – 时代Java

//package com.nowjava;

public class Main {

public static void main(String[] argv) throws Exception {

double[] a = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,

37.1234, 67.2344, 68.34534, 69.87700 };

double[] b = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,

37.1234, 67.2344, 68.34534, 69.87700 };

System.out.println(java.util.Arrays.toString(subtract(a, b)));

}

/**

* Subtracts the two arrays together (componentwise)

*

* @throws IllegalArgumentException if the

* two arrays don’t have the same length.

*/

public static double[] subtract(double[] a, double[] b) {

if (a.length != b.length) {

throw new IllegalArgumentException(

“To add two arrays, they must have the same length : “

/*

from n o w j a v a . c o m

*/

+ a.length + “, ” + b.length);

}

double[] ans = copy(a);

for (int i = 0; i

ans[i] -= b[i];

}

return (ans);

}

/**

* Subtracts the two arrays together (componentwise).

*

* @throws IllegalArgumentException if the

* two arrays don’t have the same length.

*/

public static int[] subtract(int[] a, int[] b) {

if (a.length != b.length) {

throw new IllegalArgumentException(

“To add two arrays, they must have the same length : “

+ a.length + “, ” + b.length);

}

int[] ans = copy(a);

for (int i = 0; i

ans[i] -= b[i];

}

return (ans);

}

/**

* Returns a comma delimited string representing the value of the array.

*/

public static String toString(double[] array) {

StringBuffer buf = new StringBuffer(array.length);

int i;

for (i = 0; i

buf.append(array[i]);

buf.append(‘,’);

}

buf.append(array[i]);

return buf.toString();

}

/**

* Returns a comma delimited string representing the value of the array.

*/

public static String toString(double[][] array) {

StringBuffer buf = new StringBuffer();

for (int k = 0; k

buf.append(toString(array[k]));

buf.append(System.getProperty(“line.separator”));

}

return buf.toString();

}

/**

* Returns a comma delimited string representing the value of the array.

*/

public static String toString(int[] array) {

StringBuffer buf = new StringBuffer(array.length);

int i;

for (i = 0; i

buf.append(array[i]);

buf.append(‘,’);

}

buf.append(array[i]);

return buf.toString();

}

/**

* Returns a comma delimited string representing the value of the array.

*/

public static String toString(int[][] array) {

StringBuffer buf = new StringBuffer();

for (int k = 0; k

buf.append(toString(array[k]));

buf.append(System.getProperty(“line.separator”));

}

return buf.toString();

}

/**

* Returns a copy of the array.

*/

//a call to array.clone() may also work although this is a primitive type. I haven’t checked

//it even may be faster

public static int[] copy(int[] array) {

int[] result;

result = new int[array.length];

System.arraycopy(array, 0, result, 0, array.length);

return result;

}

/**

* Returns a copy of the array.

*/

//a call to array.clone() may also work although this is a primitive type. I haven’t checked

//it even may be faster

public static long[] copy(long[] array) {

long[] result;

result = new long[array.length];

System.arraycopy(array, 0, result, 0, array.length);

return result;

}

/**

* Returns a copy of the array.

*/

//a call to array.clone() may also work although this is a primitive type. I haven’t checked

//it even may be faster

public static float[] copy(float[] array) {

float[] result;

result = new float[array.length];

System.arraycopy(array, 0, result, 0, array.length);

return result;

}

/**

* Returns a copy of the array.

*/

/**代码未完, 请加载全部代码(NowJava.com).**/


推荐阅读
  • Java太阳系小游戏分析和源码详解
    本文介绍了一个基于Java的太阳系小游戏的分析和源码详解。通过对面向对象的知识的学习和实践,作者实现了太阳系各行星绕太阳转的效果。文章详细介绍了游戏的设计思路和源码结构,包括工具类、常量、图片加载、面板等。通过这个小游戏的制作,读者可以巩固和应用所学的知识,如类的继承、方法的重载与重写、多态和封装等。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • JDK源码学习之HashTable(附带面试题)的学习笔记
    本文介绍了JDK源码学习之HashTable(附带面试题)的学习笔记,包括HashTable的定义、数据类型、与HashMap的关系和区别。文章提供了干货,并附带了其他相关主题的学习笔记。 ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • Commit1ced2a7433ea8937a1b260ea65d708f32ca7c95eintroduceda+Clonetraitboundtom ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • Spring学习(4):Spring管理对象之间的关联关系
    本文是关于Spring学习的第四篇文章,讲述了Spring框架中管理对象之间的关联关系。文章介绍了MessageService类和MessagePrinter类的实现,并解释了它们之间的关联关系。通过学习本文,读者可以了解Spring框架中对象之间的关联关系的概念和实现方式。 ... [详细]
  • 海马s5近光灯能否直接更换为H7?
    本文主要介绍了海马s5车型的近光灯是否可以直接更换为H7灯泡,并提供了完整的教程下载地址。此外,还详细讲解了DSP功能函数中的数据拷贝、数据填充和浮点数转换为定点数的相关内容。 ... [详细]
  • Android系统源码分析Zygote和SystemServer启动过程详解
    本文详细解析了Android系统源码中Zygote和SystemServer的启动过程。首先介绍了系统framework层启动的内容,帮助理解四大组件的启动和管理过程。接着介绍了AMS、PMS等系统服务的作用和调用方式。然后详细分析了Zygote的启动过程,解释了Zygote在Android启动过程中的决定作用。最后通过时序图展示了整个过程。 ... [详细]
  • 基于Socket的多个客户端之间的聊天功能实现方法
    本文介绍了基于Socket的多个客户端之间实现聊天功能的方法,包括服务器端的实现和客户端的实现。服务器端通过每个用户的输出流向特定用户发送消息,而客户端通过输入流接收消息。同时,还介绍了相关的实体类和Socket的基本概念。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
author-avatar
陈柏菱皇君怡惠
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有