zoukankan      html  css  js  c++  java
  • java Arrays源码浅出

    1、toString 返回指定数组内容的字符串表示形式。

    demo:

    由demo可窥见Arrays.toString的所做的工作就是将数组元素转换为字符串(以逗号分割数组元素,包裹在方括号中)。

    源码:

     1 public static String toString(int[] a) {
     2     // 数组为null,则返回null
     3     if (a == null)
     4         return "null";
     5     int iMax = a.length - 1;
     6     // 空数组,则返回"[]"
     7     if (iMax == -1)
     8         return "[]";
     9     
    10     // 创建StringBuilder对象,用于存放结果
    11     StringBuilder b = new StringBuilder();
    12     // 返回结果以左中括号([)开头
    13     b.append('[');
    14     // 遍历数组
    15     for (int i = 0; ; i++) {
    16         b.append(a[i]);
    17         if (i == iMax)
    18             return b.append(']').toString();// 中括号结束
    19         b.append(", "); // 逗号分割
    20     }
    21 }
    View Code

    2、

    demo:

    源码:

     1 public static String deepToString(Object[] a) {
     2     if (a == null)
     3         return "null";
     4 
     5     int bufLen = 20 * a.length;
     6     // java数组用int类型数据作索引,理论最大长度Integer.MAX_VALUE。
     7     // 如果数组足够大,则 20 * a.length 可能会溢出
     8     if (a.length != 0 && bufLen <= 0)
     9         bufLen = Integer.MAX_VALUE; // 若溢出,就取 Integer.MAX_VALUE
    10     //创建StringBuilder用来存放结果
    11     StringBuilder buf = new StringBuilder(bufLen);
    12     deepToString(a, buf, new HashSet<>());
    13     return buf.toString();
    14 }
    15 
    16 private static void deepToString(Object[] a, StringBuilder buf,
    17                                  Set<Object[]> dejaVu) {
    18     if (a == null) {
    19         buf.append("null");
    20         return;
    21     }
    22     int iMax = a.length - 1;
    23     if (iMax == -1) {
    24         buf.append("[]");
    25         return;
    26     }
    27 
    28     dejaVu.add(a);
    29     buf.append('[');
    30     // 遍历数组
    31     for (int i = 0; ; i++) {
    32 
    33         // 取数组元素
    34         Object element = a[i];
    35         if (element == null) {
    36             buf.append("null");
    37         } else { // 数组元素不为 null
    38             // 获取数组元素的类型类
    39             Class<?> eClass = element.getClass();
    40 
    41             if (eClass.isArray()) {  // 数组的元素仍然是数组
    42                 // 8种基本数据类型,直接调用Arrays.toString处理
    43                 if (eClass == byte[].class)
    44                     buf.append(toString((byte[]) element));
    45                 else if (eClass == short[].class)
    46                     buf.append(toString((short[]) element));
    47                 else if (eClass == int[].class)
    48                     buf.append(toString((int[]) element));
    49                 else if (eClass == long[].class)
    50                     buf.append(toString((long[]) element));
    51                 else if (eClass == char[].class)
    52                     buf.append(toString((char[]) element));
    53                 else if (eClass == float[].class)
    54                     buf.append(toString((float[]) element));
    55                 else if (eClass == double[].class)
    56                     buf.append(toString((double[]) element));
    57                 else if (eClass == boolean[].class)
    58                     buf.append(toString((boolean[]) element));
    59                 else { // element is an array of object references
    60                     // 循环引用,制防止死循环
    61                     if (dejaVu.contains(element))
    62                         buf.append("[...]");
    63                     else // 非基本数据类型(基本数据类型的包装类,或其他对象),则继续递归处理
    64                         deepToString((Object[])element, buf, dejaVu);
    65                 }
    66             } else {  // element is non-null and not an array
    67                 // 数组元素不是null且不是一个数组,则直接调用元素的toString方法
    68                 // 如果是包装类,则调用其类中的toString;
    69                 // 如果是其他对象,则调用对象重写过的的toString,否则调用Object.toString
    70                 buf.append(element.toString());
    71             }
    72         }
    73         if (i == iMax)
    74             break;
    75         buf.append(", ");
    76     }
    77     buf.append(']');
    78     dejaVu.remove(a);
    79 }
    View Code

     3、copyOf 从原数组复制指定长度到新数组。

    demo:

    源码:

    public static int[] copyOf(int[] original, int newLength) {
            // newLength长度的新数组
            int[] copy = new int[newLength];
            // 复制原数组到新数组
            System.arraycopy(original, 0, copy, 0,
                             Math.min(original.length, newLength));
            return copy;
        }

    java.lang.System.arraycopy() 方法复制指定的源数组的数组,在指定的位置开始,到目标数组的指定位置。如下:

    public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
      Object src : 原数组
    int srcPos : 从元数据的起始位置开始
      Object dest : 目标数组
      int destPos : 目标数组的开始起始位置
      int length : 要copy的数组的长度

     4、

    ---恢复内容结束---

  • 相关阅读:
    sql性能查询
    ASP.Net Web应用程序与EXCEL交互时遇到的权限问题
    Connection strings for Excel 2007
    获取异常的具体出处dbms_utility.format_error_backtrace
    C#获取Excel架构信息的方法
    Oracle强杀进程
    C#游标溢出(访问数据库)解决方案。
    Visual C# 2008 调试技巧一
    【POI】修改Excel内容
    【Eclipse】在Eclipse工具中自定义类注释
  • 原文地址:https://www.cnblogs.com/natian-ws/p/10265365.html
Copyright © 2011-2022 走看看