2016-05-18 4 views
-2

こんにちは、私はこれにかなり新しく、このコードについて質問があります。私は出力がどのようになっているかについては確信しています。誰かが初心者にそれを説明できるなら、私は感謝するでしょう!これはコードです:Javaコードの内容を理解する

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です。

+4

デバッガでコードをステップ実行し、その動作を観察する良い機会のように見えます。 – David

答えて

1

最初の関数では、新しい配列を作成しています。この配列は返されますが、値は決してオリジナルにコピーされません。行列を印刷するときに、あなたがこれを変更したい場合はそのため最初のループは、ラインを切り替える1、2、3として滞在します:

first(matrix[0]); 

へ:

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

第二の機能は、元の配列を受け取りa.lengthが3で3/2 = 1であるため、1回ループします。このループ中、a [0]を[a.length - 1 - i]または[3 - 1 - 0]に変換します。次に[a.length-1-i]またはa [2]をa [0]のtempに切り替えます。これにより、配列の最初と最後の6と4の要素が切り替わります。

+0

ありがとうございます。私は完全に最初の機能の '新しい'キーワードを逃した –

+0

喜んで助けてください。配列を簡単に印刷するには、Arrays.toString()メソッドを試してみてください。この場合、内側forループをArrays.toString(matrix [i])に置き換えます。 – duncan

0

最初はコードがかなり乱雑です。適切な字下げを使用するようにしてください(ブロックごとに4つのスペース)。それはコードを読みやすくします(あなた自身が答えを見つけたかもしれません)。

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です。