有人可以解释一下当字符串转换为字节数组时会发生什么吗?它发生了什么以及如何向这个字节数组添加更多字符串?
例如:火箭很有趣。
最佳答案
我认为之前的文章很好地介绍了字符串在 16 位与 8 位表示方面发生的情况。您问题的后半部分,关于增长字节数组,通常使用 System.arraycopy(src, srcPos, dest, destPos, length) 执行。
String str="Rockets are fun.";
byte[] ba=str.getBytes();
byte[] bigger=new byte[23];
System.arraycopy(ba, 0, bigger, 0, ba.length);
byte[] toFly=" to fly.".getBytes();
System.arraycopy(toFly, 0, bigger, 15, toFly.length);
System.out.println(new String(bigger, "UTF-8"));
关于java - java中的字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8147198/