2016-07-24 10 views
-2

私はこのコードを使用しています:奇妙な出力印刷する配列

import java.util.Scanner; 
import java.util.Arrays; 
import java.util.Random; 

public class Matrix 
{ 
    public static void main(String[] args) 
    { 
     Scanner enter = new Scanner(System.in); 
     int[][] firstMatrix = new int[4][4]; 
     int[][] secondMatrix = new int[4][4]; 
     Random spin = new Random(); 



     System.out.println("Please enter 16 numbers to populate the array: "); 

     for (int count = 0; count<firstMatrix.length;count++) // nested for for user input to populate 4x4 array 
     { 


      for (int count2 = 0; count2 < firstMatrix[count].length; count2++) 
      { 

       firstMatrix[count][count2] = enter.nextInt(); // populate array with user input 



      } 

     } // end array population 

      System.out.printf("The sum is: %d%n", sumMatrix(firstMatrix)); // call sumMatrix 
      System.out.println(firstMatrix[0][0]); // debug 


      for (int count3 = 0; count3<secondMatrix.length; count3++) // nested for to populate array with random numbers 1-100, row 
      { 
       for (int count4 = 0; count4<secondMatrix[count3].length; count4++) // column 
       { 
        secondMatrix[count3][count4] = 1 + spin.nextInt(100); // 100 inclusive, generate and populate array 
       } 
      } 

      System.out.println(secondMatrix[0][0]); // debug to show that it is properly printing the correct element 

      for (int i = 0; i<secondMatrix.length; i++) 
      { 
       for (int j = 0; j<secondMatrix[i].length; j++) 
        System.out.print(" " + secondMatrix[i][j]); // print the total array (this process can be used to print the returned array) 


      } 

     System.out.println(); // debug 

      int arrayTotal = firstMatrix[0][0] + secondMatrix[0][0]; // debug 
       System.out.println("The element total is " + arrayTotal); // debug 
        System.out.println(); 

     addMatrix(firstMatrix,secondMatrix); // call addMatrix 






     System.out.println("Programmed by Stephen Mills"); 
    } // end main  

     public static int sumMatrix(int[][] array) // method to sum the elements of the array 
     { 
       int total = 0; 

       for (int number = 0; number<array.length; number++) // row 
       { 
        for (int number2 = 0; number2<array[number].length; number2++) // column 
         total += array[number][number2]; // sum of all elements 
       } 
        return total; // returns the sum 

     } // end sumMatrix 

     public static int[][] addMatrix(int[][] array1, int[][] array2) // improper method 
     { 
      int[][] thirdMatrix = new int[4][4]; 

      for (int count4 = 0; count4<thirdMatrix.length; count4++) 
      { 
       for (int count5 = 0; count5<thirdMatrix[count4].length; count5++) 
        thirdMatrix[count4][count5] = array1[count4][count5]+array2[count4][count5]; 


      } 
      System.out.println(Arrays.toString(thirdMatrix)); 
      return thirdMatrix; 
     } // end addMatrix7 



} 

プログラムのほとんどの作品意図したものではなく、私はこの「[[I @ 3d4eac69を得ているように、[I @ 42a57993、[I @ 75b84c92、配列を印刷しようとすると[I @ 6bc7c054 "が出力されます。私は単純なprintln、toString、メソッド呼び出しと同じ変数を設定するなど、いくつかのメソッドを試しました。そしてprintステートメントを最初にmainに入れてから、メソッドを2番目に入れてみました。これがなぜ起こっているのか?

答えて

0

あなたはこれを試すことができthirdMatrix 2次元配列の要素をint印刷する場合:

for(int[] simpleArray: thirdMatrix) 
     System.out.println(Arrays.toString(simpleArray)); 

あなたの出力の理由を:あなたは

Arrays.toString(thirdMatrix) 

を呼び出すときは、

を呼び出します
String.valueOf(element) 

thirdMatrixのforeach要素。 thirdMatrixの要素が配列ではないプリミティブ型、

String.valueOf(Object obj) 

が呼び出されるメソッドであるようthirdMatrix要素(1次元配列)のハッシュコードを返しています。

+0

うわー。これは私の問題を解決しました。私がどれほど感謝しているかは分かりません。私はこのプロジェクトを終日続けています。これを回答としてどのように設定するのですか?申し訳ありませんが、最初の投稿。編集:よく、もう一つ、あなたはこのメソッドを使用してprintfを使用することができますか?もしそうなら、どんな書式指定子を使用しますか?ありがとうございました! – srmjr

+0

printfはネストされたforループで次のように使用できます。 for(int [] OneDArray:thirdMatrix) for(int要素:OneDArray) \t System.out.printf( "%d"、要素); int値には "%d"が使用されます。 –