package algorithm.unit1;
import java.util.Arrays;
/**
* Created by lwb on 2015/3/13.
*/
public class Array {
/**
* 获取数组最大值
*/
public static double getMax(double[] array) {
double max = array[0];
for (int i = 0; i < array.length; i++)
if (array[i] > max)
max = array[i];
return max;
}
/**
* 计算数组元素平均值
*/
public static double getAverage(double[] array) {
double total = 0;
int length = array.length;
for (int i = 0; i < length; i++)
total += array[i];
return total / length;
}
/**
* 复制数组
*/
public static double[] getCopy(double[] array) {
int length = array.length;
double[] copy = new double[length];
for (int i = 0; i < length; i++)
copy[i] = array[i];
return copy;
}
/**
* 颠倒数组
*/
public static double[] getReverse(double[] array) {
int length = array.length;
for (int i = 0; i < length / 2; i++) {
double temp = array[i];
array[i] = array[length - i - 1];
array[length - 1 - i] = temp;
}
return array;
}
/**
* 矩阵相乘
*/
public static double[][] getProduct(double[][] a1, double[][] a2) {
int line = a1.length;
int row = a2[0].length;
if (a1.length == a2[0].length && a1[0].length == a2.length) {
double[][] product = new double[line][row];
for (int i = 0; i < line; i++) {
for (int j = 0; j < row; j++) {
for (int k = 0; k < a1[0].length; k++)
product[i][j] += a1[i][k] * a2[k][j];
}
}
return product;
} else throw new IllegalArgumentException("参数错误");
}
public static void main(String[] args) {
double[][] a1 = {{1,2},{3,4}};
double[][] a2= {{1,2},{3,4}};
System.out.println("test");
System.out.println(Arrays.deepToString(Array.getProduct(a1, a2)));
}
}
Arrays.deepToString()方法:
public static String deepToString(Object[] a) {
if (a == null)
return "null";
int bufLen = 20 * a.length;
if (a.length != 0 && bufLen <= 0)
bufLen = Integer.MAX_VALUE;
StringBuilder buf = new StringBuilder(bufLen);
deepToString(a, buf, new HashSet<Object[]>());
return buf.toString();
}
private static void deepToString(Object[] a, StringBuilder buf,
Set<Object[]> dejaVu) {
if (a == null) {
buf.append("null");
return;
}
int iMax = a.length - 1;
if (iMax == -1) {
buf.append("[]");
return;
}
dejaVu.add(a);
buf.append('[');
for (int i = 0; ; i++) {
Object element = a[i];
if (element == null) {
buf.append("null");
} else {
Class eClass = element.getClass();
if (eClass.isArray()) {
if (eClass == byte[].class)
buf.append(toString((byte[]) element));
else if (eClass == short[].class)
buf.append(toString((short[]) element));
else if (eClass == int[].class)
buf.append(toString((int[]) element));
else if (eClass == long[].class)
buf.append(toString((long[]) element));
else if (eClass == char[].class)
buf.append(toString((char[]) element));
else if (eClass == float[].class)
buf.append(toString((float[]) element));
else if (eClass == double[].class)
buf.append(toString((double[]) element));
else if (eClass == boolean[].class)
buf.append(toString((boolean[]) element));
else { // element is an array of object references
if (dejaVu.contains(element))
buf.append("[...]");
else
deepToString((Object[])element, buf, dejaVu);
}
} else { // element is non-null and not an array
buf.append(element.toString());
}
}
if (i == iMax)
break;
buf.append(", ");
}
buf.append(']');
dejaVu.remove(a);
}