Array基本操作方法

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);
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值