Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法将字符附加到StringBuffer二维数组_Java_Arrays_Char_Append_Stringbuffer - Fatal编程技术网

Java 无法将字符附加到StringBuffer二维数组

Java 无法将字符附加到StringBuffer二维数组,java,arrays,char,append,stringbuffer,Java,Arrays,Char,Append,Stringbuffer,有人知道我为什么不能在下面的示例中向这个StringBuffer数组追加一个字符吗?有人能告诉我该怎么做吗 public class test { public static void main(String args[]){ StringBuffer[][] templates = new StringBuffer[3][3]; templates[0][0].append('h'); } } 我对这段代码的输出是: output:

有人知道我为什么不能在下面的示例中向这个StringBuffer数组追加一个字符吗?有人能告诉我该怎么做吗

public class test {
    public static void main(String args[]){

        StringBuffer[][] templates = new StringBuffer[3][3];

        templates[0][0].append('h');
    }
}
我对这段代码的输出是:

output:     Exception in thread "main" java.lang.NullPointerException
            at test.main(test.java:6)

如果您知道任何解决方案,请对此作出回应。您需要在附加内容之前初始化缓冲区


模板[0][0]=新建StringBuffer

在附加内容之前,需要初始化缓冲区


模板[0][0]=新建StringBuffer

下面的语句将仅声明数组,但不会初始化其元素:

    StringBuffer[][] templates = new StringBuffer[3][3];
在尝试将内容附加到数组元素之前,需要初始化它们。不这样做将导致NullPointerException

添加此初始化

    templates[0][0] = new StringBuffer();
然后追加

    templates[0][0].append('h');

下面的语句将仅声明一个数组,但不会初始化其元素:

    StringBuffer[][] templates = new StringBuffer[3][3];
在尝试将内容附加到数组元素之前,需要初始化它们。不这样做将导致NullPointerException

添加此初始化

    templates[0][0] = new StringBuffer();
然后追加

    templates[0][0].append('h');

其他人正确地指出了正确的答案,但当您尝试执行模板[1][2]之类的操作时会发生什么呢

你真正需要的是这样的东西:

public class Test {          //<---Classes should be capitalized.

    public static final int ARRAY_SIZE = 3;  //Constants are your friend.

    //Have a method for init of the double array
    public static StringBuffer[][] initArray() {
       StringBuffer[][] array = new StringBuffer[ARRAY_SIZE][ARRAY_SIZE];
       for(int i = 0;i<ARRAY_SIZE;i++) {
            for(int j=0;j<ARRAY_SIZE;j++) array[i][j] = new StringBuffer();
        }
        return array;
    }

    public static void main(String args[]){

       StringBuffer[][] templates = initArray();

        templates[0][0].append('h');
        //You are now free to conquer the world with your StringBuffer Matrix.
    }
}

使用常数很重要,因为期望矩阵大小发生变化是合理的。通过使用常量,您可以只在一个位置更改它,而不是分散在整个程序中。

其他人正确地指出了正确的答案,但是当您尝试执行类似模板[1][2]的操作时会发生什么情况。请附加“h”

你真正需要的是这样的东西:

public class Test {          //<---Classes should be capitalized.

    public static final int ARRAY_SIZE = 3;  //Constants are your friend.

    //Have a method for init of the double array
    public static StringBuffer[][] initArray() {
       StringBuffer[][] array = new StringBuffer[ARRAY_SIZE][ARRAY_SIZE];
       for(int i = 0;i<ARRAY_SIZE;i++) {
            for(int j=0;j<ARRAY_SIZE;j++) array[i][j] = new StringBuffer();
        }
        return array;
    }

    public static void main(String args[]){

       StringBuffer[][] templates = initArray();

        templates[0][0].append('h');
        //You are now free to conquer the world with your StringBuffer Matrix.
    }
}

使用常数很重要,因为期望矩阵大小发生变化是合理的。通过使用常量,您可以只在一个位置更改它,而不是分散在整个程序中。

代码按设计工作。代码按设计工作。