Java 如何打印填充有1和0的二维数组?

Java 如何打印填充有1和0的二维数组?,java,Java,我正在尝试打印一个带有1和0的2D数组(大小由用户指定)。然而,每次我尝试跑步时,我都会得到随机数字和字母,如“[[I@4eec7777“而不是2D数组。我注释掉了for循环,并认为我已将问题缩小到数组的初始化?我不确定我做错了什么 System.out.print("How many rows? : "); int numRows = userInput.nextInt(); //numRows works System.out.print(&quo

我正在尝试打印一个带有1和0的2D数组(大小由用户指定)。然而,每次我尝试跑步时,我都会得到随机数字和字母,如“[[I@4eec7777“而不是2D数组。我注释掉了for循环,并认为我已将问题缩小到数组的初始化?我不确定我做错了什么

    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);
System.out.print(“多少行?:”);
int numRows=userInput.nextInt();//numRows有效
System.out.print(“多少列?:”);
int numCols=userInput.nextInt();//numCols有效
int randomArray[][]=新的int[numRows][numCols];
//for(int row=0;row
问题 数组不重写Java中的toString()方法。 如果您尝试直接打印一个,您将获得数组哈希代码的类名+@+十六进制,如Object.toString()所定义

解决方案 由于Java 5,您可以对数组中的数组使用Arrays.toString(arr)或Arrays.deepToString(arr)。请注意,对象[]版本对数组中的每个对象调用.toString()

因此,您可以通过以下方式轻松打印嵌套数组:

System.out.println(Arrays.deepToString(randomArray));
这里是完整的代码

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));
    }
    
}
import java.util.*;
公务舱{
公共静态void main(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
System.out.print(“多少行?:”);
int numRows=userInput.nextInt();//numRows有效
System.out.print(“多少列?:”);
int numCols=userInput.nextInt();//numCols有效
int randomArray[][]=新的int[numRows][numCols];
for(int row=0;row
问题 数组不重写Java中的toString()方法。 如果您尝试直接打印一个,您将获得数组哈希代码的类名+@+十六进制,如Object.toString()所定义

解决方案 由于Java 5,您可以对数组中的数组使用Arrays.toString(arr)或Arrays.deepToString(arr)。请注意,对象[]版本对数组中的每个对象调用.toString()

因此,您可以通过以下方式轻松打印嵌套数组:

System.out.println(Arrays.deepToString(randomArray));
这里是完整的代码

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));
    }
    
}
import java.util.*;
公务舱{
公共静态void main(字符串[]args){
扫描仪用户输入=新扫描仪(System.in);
System.out.print(“多少行?:”);
int numRows=userInput.nextInt();//numRows有效
System.out.print(“多少列?:”);
int numCols=userInput.nextInt();//numCols有效
int randomArray[][]=新的int[numRows][numCols];
for(int row=0;row
要打印数组,你应该使用Arrays.toString()你必须导入数组。对于嵌套数组deepToString方法,你不能像那样只打印数组,你需要循环来索引数组的内容。它在下面被注释掉,它将循环遍历每一行和每一列并赋值(0或1)。我很确定这是可行的?您好!如果好奇的话:@后面显示的值是数组哈希代码的十六进制表示形式。这是因为数组不重写Object.toString(),所以调用了默认方法,它将显示:
getClass().getName()+'@'+Integer.toHexString(hashCode())
老实说,我不知道这意味着什么,去年才开始编写java;)要打印数组,应该使用Arrays.toString()您必须导入数组。对于嵌套数组deepToString方法,您不能像那样只打印数组,您需要循环来索引数组的内容。它在下面被注释掉,它将循环遍历每一行和每一列并指定一个值(0或1)。我很确定这是可行的?您好!如果好奇的话:@后面显示的值是数组哈希代码的十六进制表示形式。这是因为数组不重写Object.toString(),所以调用了默认方法,它将显示:
getClass().getName()+'@'+Integer.toHexString(hashCode())
老实说,我不知道这意味着什么,去年才开始编写java;)谢谢,但当我尝试这样做时,它给出了一个错误:在“数组”上找不到符号是的,这让我等了一段时间才做正确的标记。我给出了一点上下文。现在看起来不错。水晶clear@aran谢谢:)谢谢,但当我尝试这样做时,它给出了一个错误:在“数组”上找不到符号。是的,它让我在标记为正确之前等待了一段时间。我给出了一点上下文。它现在看起来不错。crystalclear@aran谢谢:)