本篇博客主要涉及了整数举证的加法和乘法运算,说到底是二维数组的遍历,逻辑很简单,且之前也已经提过,这里就不再赘述了。学到的更多是Java语法层面的知识,包括异常机制、this()的用法、类方法与实例方法。
代码

package day17;import java.util.Arrays;public class IntMatrix {/*** The data.*/int[][] data;/*** ********************** The first constructor.* * @param paraRows    The number of rows.* @param paraColumns The number of columns.***********************/public IntMatrix(int paraRows, int paraColumns) {data = new int[paraRows][paraColumns];}// Of the first constructor/*** ********************** The second constructor.Construct a copy of given matrix.* * @param paraMatrix The given matrix.***********************/public IntMatrix(int[][] paraMatrix) {data = new int[paraMatrix.length][paraMatrix[0].length];// Copy elementsfor (int i = 0; i < data.length; i++) {for (int j = 0; j < data[0].length; j++) {data[i][j] = paraMatrix[i][j];} // Of for j} // Of for i}// Of the second constructor/*** ********************** The third constructor.Construct a copy of the given matrix.* * @param paraMatrix***********************/public IntMatrix(IntMatrix paraMatrix) {this(paraMatrix.getData());}// Of the third matrix/*** ********************** @Title: getIdentityMatrix* @Description: TODO(Get identity matrix. The value at the diagonal are all 1)** @param paraRows The given rows.***********************/public static IntMatrix getIdentityMatrix(int paraRows) {IntMatrix resulMatrix = new IntMatrix(paraRows, paraRows);for (int i = 0; i < paraRows; i++) {// According to access control,resultMatrix.data can be visited directly.resulMatrix.data[i][i] = 1;} // Of for ireturn resulMatrix;}// Of getIdentityMatrix/*** Overrides the method claimed in Object,the superclass of any class.*/public String toString() {return Arrays.deepToString(data);}// Of toString/*** ********************** @Title: getData* @Description: TODO(Get my data. Warning,the reference to the data instead if*               a copy of the data is returned.)** @return The data matrix.***********************/public int[][] getData() {return data;}// Of get Data/*** ********************** @Title: getRows* @Description: TODO(Getter)** @return The number of rows.***********************/public int getRows() {return data.length;}// Of getRows/*** ********************** @Title: getColumns* @Description: TODO(Getter)** @return The number of columns.***********************/public int getColumns() {return data[0].length;}// Of getColumns/*** ********************** @Title: setValue* @Description: TODO(Set on the value of one element.)** @param paraRow    The row of the element.* @param paraColumn The column of the element.* @param paraValue  The new value.***********************/public void setValue(int paraRow, int paraColumn, int paraValue) {data[paraRow][paraColumn] = paraValue;}// Of setValue/*** ********************** @Title: getValue* @Description: TODO(Get the value of one element.)** @param paraRow    The row of the element.* @param paraColumn The column of the element.***********************/public int getValue(int paraRow, int paraColumn) {return data[paraRow][paraColumn];}// Of getValue/*** ********************** @Title: add* @Description: TODO(Add another matrix to me.)** @param paraMatrix The other matrix.***********************/public void add(IntMatrix paraMatrix) throws Exception {// Step 1.Get the data of the given matrix.int[][] tempData = paraMatrix.getData();// Step 2.Size check.if (data.length != tempData.length) {throw new Exception("Cannot add matrices. Row not match: " + data.length + " vs. " + tempData.length);} // Of ifif (data[0].length != tempData[0].length) {throw new Exception("Cannot add matrices. Column not match: " + data[0].length + " vs. " + tempData[0].length);} // Of if// Step 3.Add to me.for (int i = 0; i < data.length; i++) {for (int j = 0; j < data[0].length; j++) {data[i][j] += tempData[i][j];} // Of for j} // Of for i}// Of add/*** ********************** @Title: add* @Description: TODO(Add two existing matrices.)** @param paraMatrix1 The first matrix.* @param paraMatrix2 The second matrix.* @return A new matrix.***********************/public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {// Step 1.Clone the first matrix.IntMatrix resultMatrix = new IntMatrix(paraMatrix1);// Step 2.Add the second one.resultMatrix.add(paraMatrix2);return resultMatrix;}// Of add/*** ********************** @Title: multiply* @Description: TODO(Multiply two existing matrices)** @param paraMatrix1 The first matrix.* @param paraMatrix2 The second matrix.* @return* @throws Exception***********************/public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {// Step 1.Check size.int[][] tempData1 = paraMatrix1.getData();int[][] tempData2 = paraMatrix1.getData();if (tempData1[0].length != tempData2.length) {throw new Exception("Canot multiply matrices: " + tempData1[0].length + " vs. " + tempData2.length + ".");} // Of if// Step 2.Allocate space.int[][] resultData = new int[tempData1.length][tempData2[0].length];// Step 3.Multiplyfor (int i = 0; i < tempData1.length; i++) {for (int j = 0; j < tempData2[0].length; j++) {for (int k = 0; k < tempData1[0].length; k++) {resultData[i][j] += tempData1[i][k] * tempData2[k][j];} // Of for k} // Of for j} // Of for iIntMatrix resultIntMatrix = new IntMatrix(resultData);return resultIntMatrix;}// Of multiply/*** ********************** @Title: main* @Description: TODO(The entrance of the program)** @param args Not used now***********************/public static void main(String args[]) {IntMatrix tempMatrix1 = new IntMatrix(3, 3);tempMatrix1.setValue(0, 1, 1);tempMatrix1.setValue(1, 0, 1);tempMatrix1.setValue(1, 2, 1);tempMatrix1.setValue(2, 1, 1);System.out.println("The original matrix is: " + tempMatrix1);IntMatrix tempMatrix2 = null;try {tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);} catch (Exception e) {System.out.println(e);} // Of trySystem.out.println("The square matrix is: " + tempMatrix2);IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);try {tempMatrix3.add(tempMatrix1);} catch (Exception e) {System.out.println(e);} // Of trySystem.out.println("The connectivity matrix is: " + tempMatrix3);IntMatrix tempMatrix4 = new IntMatrix(3, 2);try {tempMatrix2 = IntMatrix.add(tempMatrix1, tempMatrix4);System.out.println("The square matrix is: " + tempMatrix2);} catch (Exception e) {System.out.println(e);} // Of try}// Of main
}// Of class IntMatrix

运行结果:

Day17——整数矩阵及其运算相关推荐

  1. python数值运算实例_Python矩阵常见运算操作实例总结

    本文实例讲述了Python矩阵常见运算操作.分享给大家供大家参考,具体如下: python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包. 一.numpy的导入 ...

  2. python矩阵运算_Python矩阵常见运算操作实例总结

    本文实例讲述了Python矩阵常见运算操作.分享给大家供大家参考,具体如下: python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包. 一.numpy的导入 ...

  3. Matlab矩阵幂运算

    Matlab帮助文档 help mpower ^ Matrix power.Z = X^y is X to the y power if y is a scalar and X is square. ...

  4. MATLAB求矩阵的100次方,Matlab矩阵幂运算

    Matlab帮助文档 help mpower ^ Matrix power. Z = X^y is X to the y power if y is a scalar and X is square. ...

  5. MATLAB程序设计教程(2)——MATLAB矩阵及其运算

    第2章  MATLAB矩阵及其运算 2.1  变量和数据操作 2.2  MATLAB矩阵 2.3  MATLAB运算 2.4  矩阵分析 2.5  矩阵的超越函数 2.6  字符串 2.7  结构数据 ...

  6. python矩阵运算实例_Python矩阵常见运算操作实例总结

    本文实例讲述了python矩阵常见运算操作.分享给大家供大家参考,具体如下: python的numpy库提供矩阵运算的功能,因此我们在需要矩阵运算的时候,需要导入numpy的包. 一.numpy的导入 ...

  7. 参考答案:02 矩阵及其运算

    本篇图文为<线性代数及其应用>这本教材对应习题册的参考答案. 本章主要介绍有关矩阵的知识,主要包括矩阵的基本运算(加法.数乘.乘法.乘幂.迹.转置),其中乘法最为重要,在计算机图形学中具有 ...

  8. 线性代数:第二章 矩阵及其运算

    本讲义是自己上课所用幻灯片,里面没有详细的推导过程(笔者板书推导)只以大纲的方式来展示课上的内容,以方便大家下来复习. 本章主要介绍有关矩阵的知识,主要包括矩阵的基本运算(加法.数乘.乘法.乘幂.迹. ...

  9. C#的winform矩阵简单运算

    C#的winform矩阵简单运算 程序截图 关键代码 using System; using System.Collections.Generic; using System.ComponentMod ...

  10. Eigen矩阵的运算(二)

    原文:http://blog.csdn.net/houjixin/article/details/8492841 本文主要是Eigen中矩阵和向量的算术运算,在Eigen中的这些算术运算重载了C++的 ...

最新文章

  1. pass基础架构分析
  2. Recyclerview 添加一个数组
  3. C语言的一个之前没有见过的特性
  4. 2012-4-2 通过MdiParent设置窗体最前
  5. python 操作mysql 返回字典_Python查询Mysql时返回字典结构的代码
  6. OpenCV官方文档
  7. 给技术人上的管理课:平衡和集中
  8. c++primer 5th习题12.25答案
  9. Simple IOCP Server代码解读
  10. mysqlli php7.0_php7配置mysqli并使用mysqli连接mysql
  11. JMeter测试实例
  12. RIDE指定log和report的输出目录
  13. 奥维怎么记录沿线轨迹_奥维互动地图怎么绘制路线
  14. GD32F103学习笔记(3)——新建工程
  15. 音乐盒单片机c语言课程设计,基于PIC16F887单片机数字音乐盒课程设计.doc
  16. OBS Studio是一款非常专业的视频直播录制软件,完全免费
  17. SpringBoot整合shiro实现细粒度动态权限
  18. 脱壳--00.aspack.exe
  19. Redis 进阶笔记
  20. gc buffer busy的优化

热门文章

  1. 计算机路由器无线级联配置,两个无线路由器级联怎么设置?
  2. LOCK is not allowed in stored procedures
  3. css实现跑马灯效果
  4. widows安全策略
  5. 教程二:windows api(c mfc vs2017)实现U盘插拔检测,获取U盘容量,U盘内容移动,开启和关闭U盘以及获取盘符等
  6. pytorch是什么?解释pytorch的基本概念
  7. INNODB记录格式
  8. kali源代码简单说明
  9. pdfmark生成pdf文件并下载
  10. vue PC端菜单优化(第一个菜单隐藏的时候,整个菜单都不显示)