【问题标题】:How to display a char array? Java如何显示一个字符数组?爪哇
【发布时间】:2013-11-29 14:40:27
【问题描述】:

我正在尝试显示一个多维 char 数组,但在尝试显示它时出现错误。有没有更好的方法来显示这个?我在其他地方做错了吗?我无法弄清楚我的代码有什么问题。

public class recBinTree {
private int left, right, columns, rows;
private char [][]binTree;

public recBinTree(int size){
    left = 0;
    right = size-1;
    columns = size;
    double n = Math.log(size)/Math.log(2); //convert to base 2
    rows = (int)n + 1; // # of rows = n
    char[][] binTree = new char[rows][columns];
    //populate entire array with hyphens
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            binTree[i][j] = '-';
    }//end outer for
}//end constructor

public void display(){
    System.out.print("Columns: " + columns + " ");
    System.out.print("Rows: " + rows);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            System.out.print(binTree[i][j] + " "); //error on this line
        System.out.print("\n");
    }//end outer for
}//end display()


public class Driver {
    public static void main(String[] args) {
        int size = 16;
        recBinTree tree = new recBinTree(size);
        tree.display();  //error on this call
    }
}

编辑:

对不起,这是错误!

Exception in thread "main" java.lang.NullPointerException
at ProjectThreeC.recBinTree.display(recBinTree.java:38)
at ProjectThreeC.Driver.main(Driver.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

【问题讨论】:

  • 如果您告诉我们错误是什么,您认为这对我们没有帮助吗?
  • 您遇到的错误是什么?

标签: java arrays multidimensional-array char


【解决方案1】:

尝试初始化这个 private char [][]binTree; 而不是在 con 结构 (char[][] binTree = new char[rows][columns];) 中创建一个新数组。因此,每次您尝试打印时,private char [][]binTree; 都是 null。对于打印,您可以使用以下内容:

   char[][] c = new char[2][10];
   // read the values
  //now display
    for(int i=0;i<c.length;i++){
        System.out.println(String.valueOf(c[i]));
    }

【讨论】:

  • 该死,这太傻了。我还在重新学习一些Java,那个飞过我的头顶!谢谢!
【解决方案2】:
public class recBinTree {
private int left, right, columns, rows;
private char [][]binTree;

public recBinTree(int size){
    left = 0;
    right = size-1;
    columns = size;
    double n = Math.log(size)/Math.log(2); //convert to base 2
    rows = (int)n + 1; // # of rows = n
    binTree = new char[rows][columns];
    //populate entire array with hyphens
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            binTree[i][j] = '-';
    }//end outer for
}//end constructor

public void display(){
    System.out.print("Columns: " + columns + " ");
    System.out.print("Rows: " + rows);
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < columns; j++)
            System.out.print(binTree[i][j] + " "); //error on this line
        System.out.print("\n");
    }//end outer for
}//end display()

    public static void main(String[] args) {
        int size = 16;
        recBinTree tree = new recBinTree(size);
        tree.display();  //error on this call
    }
}

【讨论】:

    【解决方案3】:

    您将char [][]binTree; 声明为实例,并再次在构造函数中声明。从构造函数中删除声明。

    private char [][]binTree;
    
    public recBinTree(int size){
         ......
       binTree = new char[rows][columns]; //Remove  char[][]  from here
        //populate entire array with hyphens
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++)
                binTree[i][j] = '-';
        }
    }
    

    另外,您不需要两个 for 循环来用 - 填充 binTree 数组。只需简单地使用Arrays.fill 操作。

     public recBinTree(int size){
         ......
       binTree = new char[rows][columns];
       Arrays.fill('-');       
    }
    

    【讨论】:

      【解决方案4】:

      这样做

          char [][]binTree={{'A','B','C'},{'D','E','F'},{'G','H','I'}};
          System.out.println(Arrays.deepToString(binTree));
      

      【讨论】:

        【解决方案5】:

        通过char[][] binTree = new char[rows][columns];,您将在函数范围内创建一个新变量。你得到一个空指针异常,因为当应用程序稍后在 binTree 变量中查找整个类的范围时,它会发现它是空的。

        改成:

        binTree = new char[rows][columns];
        

        这将为整个类的范围初始化 binTree 变量。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-17
          • 1970-01-01
          • 2011-07-23
          • 2020-06-06
          • 1970-01-01
          • 1970-01-01
          • 2022-12-03
          相关资源
          最近更新 更多