【发布时间】:2014-12-17 05:05:02
【问题描述】:
我有这样的 2 字节数组:
byte a[i]=1;
byte b[i]=0;
我想合并它,使输出结果变成“10”。我尝试使用 arraycopy 并制作新的输出
byte[] output=a.length+b.length;
但它仍然不像我的预期那样工作。有人知道怎么解决吗?
【问题讨论】:
我有这样的 2 字节数组:
byte a[i]=1;
byte b[i]=0;
我想合并它,使输出结果变成“10”。我尝试使用 arraycopy 并制作新的输出
byte[] output=a.length+b.length;
但它仍然不像我的预期那样工作。有人知道怎么解决吗?
【问题讨论】:
试试这个。
byte[] first = getFirstBytes();
byte[] second = getSecondBytes();
List<Byte> listOfBytes = new ArrayList<Byte>(Arrays.<Byte>asList(first));
listOfBytes.addAll(Arrays.<Byte>asList(second));
byte[] combinedByte = listOfBytes.toArray(new byte[listOfBytes.size()]);
【讨论】:
试试这个
byte[] first = {1,2,4,56,6};
byte[] second = {4,5,7,9,2};
byte[] merged = new byte[first.length+second.length];
System.arraycopy(first,0,merged,0,first.length);
System.arraycopy(second,0,merged,first.length,second.length);
for(int i=0; i<merged.length;i++)
{
System.out.println(merged[i]);
}
【讨论】:
试试这个。
byte a[i] = 1;
byte b[i] = 0;
byte[] output = new byte[a.length + b.length];
for(int i = 0 ; i < output.length ; ++i)
{
if(i < a.length) output[i] = a[i];
else output[i] = b[i - a.length];
}
【讨论】: