1 package com.jdk7.chapter4; 2 3 import java.text.DecimalFormat; 4 import java.util.Arrays; 5 6 /** 7 * 数组的加减乘除 8 * @author Administrator 9 * 10 */ 11 12 public class MatrixData implements Cloneable{ 13 //声明一个私有double类型的二维数组存放数据,私有变量只能在该类中使用,不能被外部调用 14 private double[][] matrixData; 15 16 //无参构造函数 17 public MatrixData(){ 18 //用默认值进行初始化 19 this.init(); 20 } 21 22 //带参构造函数 23 public MatrixData(double[][] b){ 24 //二维数组b为空则调用默认值进行初始化 25 if(!arraySameLength(b)){ 26 System.out.println("数组第二维长度不等"); 27 this.init(); 28 }else{ //二维数组b不为空,则将b克隆 29 System.out.println("数组第二维长度都相等"); 30 this.matrixData = b; 31 } 32 } 33 34 public MatrixData add(MatrixData data){ 35 if(data==null){return null;} 36 37 double[][] datab = data.getMatrixData(); 38 MatrixData c = null; 39 if(!sameLength(data)){ 40 return c; 41 }else{ 42 double[][] datac = new double[this.matrixData.length][this.matrixData[0].length]; 43 for(int i=0;i<this.matrixData.length;i++){ 44 for(int j=0;j<this.matrixData[0].length;j++){ 45 datac[i][j] = this.matrixData[i][j] + data.matrixData[i][j]; 46 } 47 } 48 c = new MatrixData(datac); 49 return c; 50 } 51 } 52 53 public MatrixData sub(MatrixData b){ 54 if(b==null){return null;} 55 double[][] datab = b.getMatrixData(); 56 MatrixData c = null; 57 if(sameLength(b)){ 58 double[][] datac = new double[this.matrixData.length][this.matrixData[0].length]; 59 for(int i=0;i<this.matrixData.length;i++){ 60 for(int j=0;j<this.matrixData[0].length;j++){ 61 datac[i][j] = this.matrixData[i][j] - datab[i][j]; 62 } 63 } 64 c = new MatrixData(datac); 65 return c; 66 }else{ 67 return c; 68 } 69 } 70 71 public MatrixData multiply(MatrixData data){ 72 if(data==null){return null;} 73 double[][] datab = data.getMatrixData(); 74 MatrixData c = null; 75 if(sameLength(data)){ 76 double[][] datac = new double[this.matrixData.length][this.matrixData[0].length]; 77 for(int i=0;i<this.matrixData.length;i++){ 78 for(int j=0;j<this.matrixData[0].length;j++){ 79 datac[i][j] = this.matrixData[i][j] * datab[i][j]; 80 } 81 } 82 c = new MatrixData(datac); 83 return c; 84 }else{ 85 return c; 86 } 87 } 88 89 public MatrixData division(MatrixData data){ 90 if(data==null){return null;} 91 double[][] datax = data.getMatrixData(); 92 MatrixData c = null; 93 if(sameLength(data)){ 94 double[][] datac = new double[this.matrixData.length][this.matrixData[0].length]; 95 for(int i=0;i<this.matrixData.length;i++){ 96 for(int j=0;j<this.matrixData[0].length;j++){ 97 if(datax[i][j]==0){ 98 System.out.println("商不能为零"); 99 break; 100 }else{ 101 datac[i][j] = (this.matrixData[i][j] / datax[i][j]); 102 } 103 } 104 } 105 c = new MatrixData(datac); 106 return c; 107 }else{ 108 return c; 109 } 110 } 111 112 //判断二维长度是否相等 113 public boolean sameLength(MatrixData b){ 114 if(this.matrixData.length != b.matrixData.length || this.matrixData[0].length != b.matrixData[0].length){ 115 return false; 116 }else{ 117 return true; 118 } 119 } 120 121 //默认的二维数组 122 private void init() { 123 this.matrixData = new double[][]{{1,2,0},{4,5,6},{7,8,9}}; 124 } 125 126 public double[][] getMatrixData() { 127 return matrixData; 128 } 129 130 public void setMatrixData(double[][] matrixData) { 131 this.matrixData = matrixData; 132 } 133 134 public Object clone(){ 135 try { 136 System.out.println("调用clone方法"); 137 MatrixData src = (MatrixData)super.clone(); 138 src.setMatrixData(this.matrixData); 139 return src; 140 } catch (CloneNotSupportedException e) { 141 e.printStackTrace(); 142 } 143 return null; 144 } 145 146 public int hashCode(){ 147 return this.toString().hashCode(); 148 } 149 150 /** 151 * 在比较的过程中涉及到多层嵌套条件判断,最外层的正反两面都要给予对应的返回结果,如果内层的返回结果同外层不一致,则需指定确切的返回结果, 152 * 反面情况保持默认的返回结果 153 */ 154 public boolean equals(Object data){ 155 if(data==null){ 156 return false; 157 } 158 if(data instanceof MatrixData){ 159 MatrixData datax = (MatrixData) data; 160 if(sameLength(datax)){ 161 for(int i=0;i<this.matrixData.length;i++){ 162 for(int j=0;j<this.matrixData[0].length;j++){ 163 if(this.matrixData[i][j] != datax.matrixData[i][j]){ 164 return false; 165 } 166 } 167 } 168 } 169 return true; 170 }else{return false;} 171 } 172 //二位数组的二维长度相等 173 public boolean arraySameLength(double[][] b){ 174 for(int i=0;i<b.length-1;i++){ 175 if(b[i].length != b[i+1].length){ 176 return false; 177 } 178 } 179 return true; 180 } 181 182 //将所有的打印信息放入StringBuffer中再转换为String类型,包括空格和换行 183 public String arrayToString(double[][] b){ 184 StringBuffer sb = new StringBuffer(""); 185 186 //按照指定的格式进行格式化 187 DecimalFormat df = new DecimalFormat(); 188 String pattern = "0.00"; 189 df.applyPattern(pattern); 190 191 for(int i=0;i<b.length;i++){ 192 for(int j=0;j<b[0].length;j++){ 193 // sb.append((Double.valueOf(b[i][j])).toString()).append(" "); 194 sb.append(df.format((Double.valueOf(b[i][j]))).toString()).append(" "); 195 } 196 sb.append("\n"); 197 } 198 return sb.toString(); 199 } 200 201 @Override 202 public String toString() { 203 // return "MatrixData [matrixData=" + Arrays.toString(matrixData) + "]"; 204 return this.arrayToString(this.matrixData); 205 } 206 207 public void display(){ 208 System.out.println(this.toString()); 209 } 210 211 }
1 package com.jdk7.chapter4; 2 3 public class Test { 4 public static void main(String[] args) { 5 double[][] array0 = new double[][]{{9.0,8.0,7.0},{6.0,5.0,4.0},{3.0,2.0,1.0}}; 6 MatrixData m0 = new MatrixData(array0); 7 MatrixData m1 = new MatrixData(); 8 System.out.print("数组m0为: "); 9 m0.display(); 10 System.out.print("数组m1为: "); 11 m1.display(); 12 System.out.print("数组(m0 + m1)为: "); 13 m0.add(m1).display(); 14 System.out.print("数组(m0 - m1)为: "); 15 m0.sub(m1).display(); 16 System.out.print("数组(m0 * m1)为: "); 17 m0.multiply(m1).display(); 18 MatrixData m2 = (MatrixData)m0.clone(); 19 System.out.print("从m0克隆的m2为: "); 20 m2.display(); 21 System.out.println("m0的哈希码为: "+m0.hashCode()); 22 System.out.println("m2的哈希码为: "+m2.hashCode()); 23 System.out.println("m0和m2是否相等: "+m0.equals(m2)); 24 System.out.print("数组m3为: "); 25 MatrixData m3 = null; 26 UsingArrays ua = new UsingArrays(); 27 System.out.println("m0和m3是否相等: "+m0.equals(m3)); 28 System.out.println("m0和m1是否相等: "+m0.equals(m1)); 29 System.out.println("m0和ua是否相等: "+m0.equals(ua)); 30 31 System.out.print("数组(m0 / m1)为: "); 32 m0.division(m1).display(); 33 } 34 35 } 36 37 38 执行结果: 39 数组第二维长度都相等 40 数组m0为: 9.00 8.00 7.00 41 6.00 5.00 4.00 42 3.00 2.00 1.00 43 44 数组m1为: 1.00 2.00 0.00 45 4.00 5.00 6.00 46 7.00 8.00 9.00 47 48 数组(m0 + m1)为: 数组第二维长度都相等 49 10.00 10.00 7.00 50 10.00 10.00 10.00 51 10.00 10.00 10.00 52 53 数组(m0 - m1)为: 数组第二维长度都相等 54 8.00 6.00 7.00 55 2.00 0.00 -2.00 56 -4.00 -6.00 -8.00 57 58 数组(m0 * m1)为: 数组第二维长度都相等 59 9.00 16.00 0.00 60 24.00 25.00 24.00 61 21.00 16.00 9.00 62 63 调用clone方法 64 从m0克隆的m2为: 9.00 8.00 7.00 65 6.00 5.00 4.00 66 3.00 2.00 1.00 67 68 m0的哈希码为: 363974377 69 m2的哈希码为: 363974377 70 m0和m2是否相等: true 71 数组m3为: m0和m3是否相等: false 72 m0和m1是否相等: false 73 m0和ua是否相等: false 74 数组(m0 / m1)为: 商不能为零 75 数组第二维长度都相等 76 9.00 4.00 0.00 77 1.50 1.00 0.67 78 0.43 0.25 0.11