java中关于StringBuffer数组的添加操作的问题

2024-12-29 11:55:56
推荐回答(4个)
回答1:

b[0]里是空的。null调用方法就会产生空指向异常。
引用类型的变量默认值是null
StringBuffer[] b = new StringBuffer[]{" "," "," "," "," "}

初始化一下,为五个空字符。空字符“ ” 调用append()不会有NullPointerException

回答2:

StringBuffer[] b = new StringBuffer[5];
b[0] = new StringBuffer();//加上这一句。你只是new初始化了数组,没有实例化数组里的对象。
b[0].append(true);
System.out.println("b[0]="+b[0]);

回答3:

StringBuffer[] b = new StringBuffer[5];
b[0] = new StringBuffer();
b[0].append(true);

回答4:

StringBuffer[] b = new StringBuffer[5];?你定义这个干吗?StringBuffer s = new StringBuffer(5);

s.append(true);