0

I'm trying to print a 2D array (size specified by user) with 1s and 0s. However, every time I try to run I get random numbers and letters like "[[I@4eec7777" instead of the 2D array. I commented out the for loops and think I have narrowed down the problem to the initialization of the array? I am not sure what I'm doing wrong.

    System.out.print("How many rows? : ");
    int numRows = userInput.nextInt(); //numRows works
    System.out.print("How many columns? : ");
    int numCols = userInput.nextInt(); //numCols works
    int randomArray[][] = new int[numRows][numCols]; 
//    for (int row = 0; row < randomArray.length; row++) {
//      int temp = (int) ((Math.random()*2)+1);
//      for (int col = 0; col < randomArray[row].length; col++) {
//        if (temp % 2 == 0) randomArray[row][col] = 1;
//      }
//    }
    System.out.println(randomArray);
David
  • 25
  • 7
  • to print Arrays you should use Arrays.toString() you have to import Arrays. For nested Array deepToString Method – Aalexander Jan 07 '21 at 21:08
  • You can't just print arrays like that, you will need loops to index over the contents of an array. – nobalG Jan 07 '21 at 21:09
  • It is commented out underneath, it will loop through each row and column and assign a value (0 or 1). I am pretty sure this works? – David Jan 07 '21 at 21:10
  • Hi! Just if curious: the value shown after @ is the hexadecimal representation of the array's hash code. That's because arrays don't override Object.toString(), so the default method is called, which will show: `getClass().getName() + '@' + Integer.toHexString(hashCode())` – aran Jan 07 '21 at 21:20
  • To be honest I have no idea what most of that means, just started coding java last year ;) – David Jan 07 '21 at 21:22
  • You'll get it soon. Welcome to java coding, it will be great, trust me : ) – aran Jan 07 '21 at 21:22

1 Answers1

2

Problem

Arrays don't override the toString() method in Java. If you try printing one directly you will get the className + @ + the hex of the hashCode of the array, as defined by Object.toString();

Solution

Since Java 5 you can use Arrays.toString(arr) or Arrays.deepToString(arr) for arrays within arrays. Note that the Object[] version calls .toString() on each object in the array

So you can easily print your nested array by

System.out.println(Arrays.deepToString(randomArray));

Here the full code

import java.util.*;
public class Whatever {
    public static void main(String[] args) {
        
        Scanner userInput = new Scanner(System.in);
        
        System.out.print("How many rows? : ");
        int numRows = userInput.nextInt(); //numRows works
        System.out.print("How many columns? : ");
        int numCols = userInput.nextInt(); //numCols works
        int randomArray[][] = new int[numRows][numCols]; 
        for (int row = 0; row < randomArray.length; row++) {
          int temp = (int) ((Math.random()*2)+1);
          for (int col = 0; col < randomArray[row].length; col++) {
            if (temp % 2 == 0) randomArray[row][col] = 1;
          }
        }
        System.out.println(Arrays.deepToString(randomArray));
    }
    
}
Aalexander
  • 4,987
  • 3
  • 11
  • 34