Gentlemen, good time of day. Such a question: why with the help of Random when outputting to the console the matrix consists of some obscure values ​​(and they are all the same), and when using the same Random for the usual number, the actual number is displayed, not the "hashcode"

package ru.itpark; import java.util.Random; import java.util.Scanner; public class Main { static int array_NxM[][]; public static void main(String[] args) { Scanner in= new Scanner(System.in); System.out.println("Введите размер матрицы"); System.out.print("Количество строк: "); int n=in.nextInt(); System.out.print("Количество столбцов: "); int m=in.nextInt(); array_NxM= new int [n][m]; Random random =new Random(); for (int i=0; i<n; i++){ for( int j=0; j<m; j++){ array_NxM[i][j]= random.nextInt(); } } for (int i=0; i<n; i++) { for (int j = 0; j < m; j++) { System.out.print(array_NxM+" "); } System.out.println(); } int b=random.nextInt(); System.out.println(); System.out.println(b); } } 

Here is the conclusion:

 Введите размер матрицы Количество строк: 5 Количество столбцов: 5 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 [[I@6e0be858 1155286982 
  • You are doing the same thing all the time. System.out.print(array_NxM[i][j]); - Igor
  • Much grateful, but before that I get a link to the array output? - Belyash
  • exactly (5 characters needed ...) - Igor
  • This is a glitch of the Matrix, clearly. - VladD

2 answers 2

Thank you, Igor. I incorrectly deduced an array, it is necessary not

 System.out.print(array_NxM+" "); 

but:

 System.out.print(array_NxM[i][j]); 

    Use array_NxM[i][j] = (int)Math.random()*n instead of Random random =new Random(); where n is the upper bound.

     public class main { static int array_NxM[][]; public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Введите размер матрицы"); System.out.print("Количество строк: "); int n = in.nextInt(); System.out.print("Количество столбцов: "); int m = in.nextInt(); array_NxM = new int [n][m]; for (int i=0; i<n; i++){ for( int j=0; j<m; j++){ array_NxM[i][j]= (int)(Math.random()*10); System.out.print(array_NxM[i][j]); } System.out.println(); } int b=(int)(Math.random()*10); System.out.println(); System.out.println(b); } } 

    All code