请教一下各位大侠,
声明了一个StringBulider对象path,现想通过append为path赋值,程序如下:StringBuilder path = new StringBuilder();
path.append(paths[j][i]);其中paths[j][i]为StringBuffer对象,值为"4,2,1"程序运行结果path并没有被复制,仍为空值。
请教下大家这是什么原因呢?
声明了一个StringBulider对象path,现想通过append为path赋值,程序如下:StringBuilder path = new StringBuilder();
path.append(paths[j][i]);其中paths[j][i]为StringBuffer对象,值为"4,2,1"程序运行结果path并没有被复制,仍为空值。
请教下大家这是什么原因呢?
//看来,咱俩用的不是同一个jdk
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
sb1.append("1").append("2").append("3");
sb2.append(sb1);
System.out.println(sb2);
}
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = new StringBuilder();
for(int i=0;i<p[i].length();i++){
if(i==p[i].length()-1){
sb1.append(p[i]);
break;
}
sb1.append(p[i]+",");
}
sb2.append(sb1);
System.out.println(sb2);
}
那么楼主确定被append进去的参数真的不是空值吗?
* @param args
*/
public static void main(String[] args) {
StringBuffer sb1=new StringBuffer();
StringBuffer sb2=new StringBuffer("1,2,3");
sb1.append(sb2);
System.out.println(sb1); }}
程序可以正常输出,说明你的paths是空的,建议你在append前先print下看看里面是不是真的有数据.
如果 paths[j][i] 中的 StringBuffer 确实有值,那么肯定会出现在 StringBuilder 中。
散分!