简单问题在线给分:怎样将一个byte[]的值拷贝给另一个byte[] byte[] a, b;...for(int i=0; i<a.length; i++) b[i] = a[i]; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 可运行, jbuilder 3 jdk1.2package csdn;public class MyClass5 { public MyClass5() { } public static void main(String[] args) { MyClass5 myClass5 = new MyClass5(); myClass5.invokedStandalone = true; myClass5.test (); } private boolean invokedStandalone = false; void test(){ byte[] a={1,0,1,0,1}; byte[] b=new byte[a.length]; System.arraycopy (a,0,b,0,a.length); try{ System.out.println ("b- "+byte_to_string (b)); }catch( Exception e) {} } static String byte_to_string ( byte[] in ) throws Exception { String result = "" ; if (in == null) return null; for (int i=0;i<in.length; i++ ) { result = result + in[i] ; } return result ; }} byte[] a,b;System.arrayCopy(b,0,a,0,a.length); byte[] a, b;...for(int i=0; i<a.length; i++) b[i] = a[i];和byte[] a,b;System.arrayCopy(b,0,a,0,a.length);都可以.循环是各种编成方式都经常采用的手法.而第二种是面向对象编成中的方便之处,在java中有System.arraycopy(Object src, int src_position, Object dst, int dst_position, int length) 这样一个方法,而byte又是对象,所以这样一来就不用自己写了,只要作为参数填写就ok了. 如果已知byte[] b;则 byte[] a = (byte[])(b.clone()); System.arrayCopy()方法不是用循环实现的,速度比用循环拷贝要快得多。 faint哪有楼上那么复杂?一个System.arraycopy就行了System.arraycopy(byte[] A,int i,byte[] B,int j,int length);意思是从数组A第i个(即A[i]处,含A[i])开始copy长度为length个的byte数据到数组B从第j个开始(即B[j]处,含B[j])覆盖! byte[] a = (byte[])(b.clone()); 实际上arraycopy是调用JNI的一个本地方法:static void cpchars(jchar *dst, char *src, int n){ int i; for (i = 0; i < n; i++) { dst[i] = src[i]; }}它也是用一个一个字符复制过来的。而用byte[] a = (byte[])(b.clone())是复制一个内存块的方式,应该更快一些才对. 最近面试遇到的三道 全英 循环 的题目 String问题 在线等高手,高手,如何用Java验证当前用户的Windows,XP,登陆密码? 准备开始学习java,请告诉我哪本说好。 请教package的问题 新手,编译不通过,请求解决? 界面设计的一个问题 Java 要怎樣才能測得一個file的大小? jb5注册机怎么用呀,各位 关于用C和java读同一个二进制文件却结果不同的问题 关于APPLET问题? 我有一个字符串,里面有一些用空格分开的数值,如何把他一次放进一个数组中?
可运行, jbuilder 3 jdk1.2package csdn;public class MyClass5 { public MyClass5() {
} public static void main(String[] args) {
MyClass5 myClass5 = new MyClass5();
myClass5.invokedStandalone = true; myClass5.test (); }
private boolean invokedStandalone = false; void test(){
byte[] a={1,0,1,0,1};
byte[] b=new byte[a.length]; System.arraycopy (a,0,b,0,a.length); try{
System.out.println ("b- "+byte_to_string (b));
}catch( Exception e) {} } static String byte_to_string ( byte[] in )
throws Exception
{
String result = "" ; if (in == null) return null; for (int i=0;i<in.length; i++ )
{
result = result + in[i] ;
} return result ;
}}
System.arrayCopy(b,0,a,0,a.length);
...
for(int i=0; i<a.length; i++)
b[i] = a[i];
和
byte[] a,b;
System.arrayCopy(b,0,a,0,a.length);都可以.
循环是各种编成方式都经常采用的手法.而第二种是面向对象编成中的方便之处,在java中有System.arraycopy(Object src, int src_position, Object dst, int dst_position, int length) 这样一个方法,而byte又是对象,所以这样一来就不用自己写了,只要作为参数填写就ok了.
哪有楼上那么复杂?
一个System.arraycopy就行了
System.arraycopy(byte[] A,int i,byte[] B,int j,int length);
意思是从数组A第i个(即A[i]处,含A[i])开始copy长度为length个的byte数据到数组B从第j个开始(即B[j]处,含B[j])覆盖!
static void cpchars(jchar *dst, char *src, int n)
{
int i;
for (i = 0; i < n; i++) {
dst[i] = src[i];
}
}
它也是用一个一个字符复制过来的。
而用byte[] a = (byte[])(b.clone())是复制一个内存块的方式,应该更快一些才对.