【问题标题】:Understanding what java code prints了解 java 代码打印的内容
【发布时间】:2016-05-18 19:44:28
【问题描述】:

大家好,我对此很陌生,对此代码有疑问。我不确定输出是怎样的,所以如果有人可以向初学者解释一下,我将不胜感激!这是代码:

public class NewClass{

    public static int[] first(int[] a) { // Array {1,2,3} is passed as an argument
        int[] b = new int[a.length]; 
        for (int i = 0; i < a.length; i++)
            b[i] = a[a.length - 1 - i];
        return b;
    }

    public static void second(int[] a) { // Use more descriptive names for you methods. If its aim is to reverse the array than call it reverseArray or something alike.
        for (int i = 0; i < a.length/2; i++) { // a.length = 3, a.length/2 = 1; So this loop will run only once
            int temp = a[i]; // temp = 4
            a[i] = a[a.length - 1 - i]; // a[0] = a[3 - 1 - 0] (a[2]) equals a[0] = 6
            a[a.length - 1 - i] = temp; // a[2] = temp equals a[2] = 4
        } // Array has become {6,5,4} (So it's been reversed.)
    }

    public static void main(String[] args) {
        int[][] matrix = {{1,2,3},{4,5,6}}; // Array of two elements, both elements refering to an other array with three elements
        System.out.println(matrix.length);  // This will print 2. It is a two dimensional array. 
        first(matrix[0]);   // Calling the first method and passing the {1,2,3} array as argument. It does stuff to a copy of the array (int[] b), but the returned value is never used. Array {1,2,3} is untouched.
        second(matrix[1]); // Same as with the first method
        for (int i = 0; i < 2; i++) { 
            for (int j = 0; j < 3; j++) // You should use { }. It will make the code easier to read. Only line 25 is executed inside this nested loop
                System.out.print(matrix[i][j]); // This will run 6 times (2 (outer loop) x 3 (nested loop)). It will print matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], matrix[1][1], matrix[1][2]. Respectively 123 (next line) 456
            System.out.println();
        }
    }
}

它输出 2,然后是 1,2,3,最后是 6,5,4。

【问题讨论】:

  • 看起来是一个在调试器中单步调试代码并观察行为的好机会。

标签: java arrays matrix


【解决方案1】:

在您的第一个函数中,您正在创建一个新数组。返回此数组,但该值永远不会复制到原始数组中。因此,在打印矩阵时,第一个循环将保持为 1、2、3。如果你想改变这一点,请切换行:

first(matrix[0]);

收件人:

matrix[0] = first(matrix[0]);

第二个函数获取原始数组并循环一次(因为 a.length 为 3 且 3 / 2 = 1)。在这个循环中,它将 a[0] 转换为 a[a.length - 1 - i] 或 a[3 - 1 - 0]。然后它将 a[a.length - 1 - i] 或 a[2] 切换到 temp 即 a[0]。这将切换数组中的第一个和最后一个元素,即 6 和 4。

【讨论】:

  • 谢谢。我完全错过了第一个函数中的“新”关键字
  • 很高兴为您提供帮助。此外,如果您想要一种更简单的方法来打印数组,请尝试使用 Arrays.toString() 方法。在这种情况下,用 Arrays.toString(matrix[i]) 替换内部 for 循环
【解决方案2】:

起初,您的代码非常混乱。尝试使用正确的缩进(每个块四个空格)。它使代码更易于阅读(您可能自己已经找到了答案)。

public class NewClass{

    public static int[] first(int[] a) { // Array {1,2,3} is passed as an argument
        int[] b = new int[a.length]; 
        for (int i = 0; i < a.length; i++)
            b[i] = a[a.length - 1 - i];
        return b;
    }

    public static void second(int[] a) { // Use more descriptive names for you methods. If its aim is to reverse the array than call it reverseArray or something alike.
        for (int i = 0; i < a.length/2; i++) { // a.length = 3, a.length/2 = 1; So this loop will run only once
            int temp = a[i]; // temp = 4
            a[i] = a[a.length - 1 - i]; // a[0] = a[3 - 1 - 0] (a[2]) equals a[0] = 6
            a[a.length - 1 - i] = temp; // a[2] = temp equals a[2] = 4
        } // Array has become {6,5,4} (So it's been reversed.)
    }

    public static void main(String[] args) {
        int[][] matrix = {{1,2,3},{4,5,6}}; // Array of two elements, both elements refering to an other array with three elements
        System.out.println(matrix.length);  // This will print 2. It is a two dimensional array. 
        first(matrix[0]);   // Calling the first method and passing the {1,2,3} array as argument. It does stuff to a copy of the array (int[] b), but the returned value is never used. Array {1,2,3} is untouched.
        second(matrix[1]); // Same as with the first method
        for (int i = 0; i < 2; i++) { 
            for (int j = 0; j < 3; j++) // You should use { }. It will make the code easier to read. Only line 25 is executed inside this nested loop
                System.out.print(matrix[i][j]); // This will run 6 times (2 (outer loop) x 3 (nested loop)). It will print matrix[0][0], matrix[0][1], matrix[0][2], matrix[1][0], matrix[1][1], matrix[1][2]. Respectively 123 (next line) 456
            System.out.println();
        }
    }
}

它输出 2,然后是 1,2,3,最后是 6,5,4。

【讨论】:

    猜你喜欢
    • 2016-05-02
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 1970-01-01
    相关资源
    最近更新 更多