基础问题,关于byte[]!

Danfor 2004-03-16 04:57:48
再c里面有
char input[31];
memset(input, 0, 31);
strcpy(input+15, "abvc4353");

再java里面怎么实现?

byte input[] = new byte[31];
然后呢?用for?有没有好的方法

谢谢!
...全文
21 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Danger2000 2004-03-17
  • 打赏
  • 举报
回复
先用Arrays.fill()用0把input[]填满,然后在从然后在用System.arraycopy()把 "abvc4353",拷贝到相应位置。 muymuy(muy)的做法就可以。
fohoo 2004-03-17
  • 打赏
  • 举报
回复
sorry,回得仓促,上面应是

Arrays.fill(input, 14, 14 + n, 0)

从14开始填充
14+n哪个不包括在填充内

如JAVADOC所说

Parameters:
a - the array to be filled.
fromIndex - the index of the first element (inclusive) to be filled with the specified value.
toIndex - the index of the last element (exclusive) to be filled with the specified value.
val - the value to be stored in all elements of the array.
fohoo 2004-03-17
  • 打赏
  • 举报
回复
查一下JAVADOC:

static void fill(byte[] a, int fromIndex, int toIndex, byte val)
Assigns the specified byte value to each element of the specified range of the specified array of bytes.

int n = 5;
Arrays.fill(input, 15, n, 0);
muymuy 2004-03-16
  • 打赏
  • 举报
回复

用System.arraycopy();

byte input[] = new byte[31];
byte [] bytes = "abvc4353".getBytes();
Arrays.fill(input, 0);
System.arraycopy(input, 15, bytes, 0, bytes.length);
myxd 2004-03-16
  • 打赏
  • 举报
回复
byte c[]=new byte[31];
String str="abvc4353";
for(int i=0;i<15;i++)c[i]=0;
for(int i=0;i<str.length();i++)c[i+15]=(byte)str.charAt(i);
Danfor 2004-03-16
  • 打赏
  • 举报
回复
楼上的两位说的不行呀,我是想让我声明的input数组里面从第15为开始存入若干个字符,其他的位置还都是'0'。
fohoo 2004-03-16
  • 打赏
  • 举报
回复
byte input[] = new byte[32];
Arrays.fill(input, 0);
flyforlove 2004-03-16
  • 打赏
  • 举报
回复
byte[] c="asdfasdf".getBytes();

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧