天天看點

Java 建立 列印二維數組

建立二維數組

  • 直接建立并指派:
int [][] array1 = {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
           
  • 指定大小進行建立
int [][] array2 = new int [4][8];//一邊聲明一邊配置設定記憶體
​
int [][] array3;
array3 = new int [4][8]; //先聲明再配置設定
​
//兩個for循環進行指派
           

列印二維數組

  • 類似一維數組的Arrays.toString(),二維數組可以直接調用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<>());
        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);
    }
           
  • for循環列印
//簡寫一下,就這意思 自己修飾下加個括号啥的
    for (i = 0; i < row; i++){
        for (j = 0; j < col; j++){
            //print(arr[i][j]);
        }
    }
    //也可以隻用一個for 稍微優化一點
    for (int i = 0, j = 0; i < a.length;) {
            System.out.print(a[i][j] +" ");
            j++;
            if (j >= a[i].length) {
                i++;
                j = 0;
            }
        }
           

好多東西不常用都忘記了,記錄一下防止遺忘

感謝看到這裡~