方法の配列にいくつかのAndroidスプライシング配列

実際のプロジェクトを合わせステッチのアレイの使用を必要とする、抽出が必要です。

ここでは、オンライン収集と照合の4つの方法があります:

A、apacheの-コモンズ

二、Arrays.copyOf

三、Array.newInstance

四、System.arraycopyの


A、apacheの-コモンズ

JDKの方法が提供されるようです。

私は遊ぶためにここに出てきません。それは出するのが面倒、少なくとも何か、と推定されています。他の3直接法。


二、ArrayscopyOf()

 public static <T> byte[] concat(byte[] first, byte[] second) {
        byte[] result = Arrays.copyOf(first, first.length + second.length);
        System.arraycopy(second, 0, result, first.length, second.length);
        return result;
    }

以下は、複数のマージされています。

public static <T> T[] concatAll(T[] first, T[]... rest) {  
  int totalLength = first.length;  
  for (T[] array : rest) {  
    totalLength += array.length;  
  }  
  T[] result = Arrays.copyOf(first, totalLength);  
  int offset = first.length;  
  for (T[] array : rest) {  
    System.arraycopy(array, 0, result, offset, array.length);  
    offset += array.length;  
  }  
  return result;  
}  

ここでの呼び出しは次のとおりです。

private byte[] aaa;
    private byte[] bbb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        aaa = new byte[5];
        bbb = new byte[5];
 byte[] ccc = concat(aaa, bbb);
        LogUtil.fussenLog().d(DensityUtils.bytesToHexString(ccc));
        LogUtil.fussenLog().d(ccc.length + "---");

これは結論System.arraycopyの()メソッドの内部で使用します


三、Array.newInstance

private static <T> byte[] concat2(byte[] a, byte[] b) {
        final int alen = a.length;
        final int blen = b.length;
        if (alen == 0) {
            return b;
        }
        if (blen == 0) {
            return a;
        }
        byte[] result = (byte[]) java.lang.reflect.Array.
                newInstance(a.getClass().getComponentType(), alen + blen);
        System.arraycopy(a, 0, result, 0, alen);
        System.arraycopy(b, 0, result, alen, blen);
        return result;
    }

ここでの呼び出しは次のとおりです。

byte[] ccc = concat2(aaa, bbb);
        LogUtil.fussenLog().d(DensityUtils.bytesToHexString(ccc));
        LogUtil.fussenLog().d(ccc.length + "---");

詳しく見てもSystem.arraycopyの()のショーがあることがわかりますその最も重要なこと

System.arraycopyの()メソッド


四、System.arraycopyの

コール:

 byte[] ccc = new byte[aaa.length + bbb.length];
        System.arraycopy(aaa, 0, ccc, 0, aaa.length);
        System.arraycopy(bbb, 0, ccc, aaa.length, bbb.length);
        LogUtil.fussenLog().d(DensityUtils.bytesToHexString(ccc));
        LogUtil.fussenLog().d(ccc.length + "---");

具体的な方法:

System.copy(オブジェクトSRC、INT srcPos、DESTオブジェクト、INT destPost、INT長さ);

最初の引数は、あなたが開始位置を、第二引数「原料」を「原料」を取るべきであるということです

3番目の引数は、あなたが「ターゲット」を置くべきであるということです、四番目のパラメータは、コピー開始位置の「ターゲット」であります

第五は、コピーしたいの長さです。


バイトの必要性[]第三開始から取り出される:要件が用意されました

System.arraycopy(output, 3, outCopy, 0, output.length - 3);
上で述べた言葉によると(ハッハッハ、直接使用する未来を容易にするため)、このようになります。


今、私は1つの要求を持っています:

outCopyは、それが一緒にステッチに変更する必要があるたびに評価になります

length = 0;
        endTime = maxWaitTime + System.currentTimeMillis();
        outCopy = null;
        while (endTime > System.currentTimeMillis()) {
            if (outCopy != null) {
                System.arraycopy(outCopy, 0, btRead, length, outCopy.length);
                length = length + outCopy.length;
                LogUtil.fussenLog().d("单次接收:" + DensityUtils.bytesToHexString(outCopy));
                outCopy = null;
                endTime = 50 + System.currentTimeMillis();
            }
        }
        LogUtil.fussenLog().d("接收:" + DensityUtils.bytesToHexString(btRead));
        btReal = new byte[length];
        System.arraycopy(btRead, 0, btReal, 0, length);
        LogUtil.fussenLog().d("接收转换:" + DensityUtils.bytesToHexString(btReal));

これは、今のコードで書かれている私のプロジェクトです。

私は第一の長さが0になる記録するために使用され、endTimeは怠惰が、これとはあまり関係が言うこと、そして、outCopyが空になるとなります

System.arraycopyの()メソッドを実行し、outCopyの原料がゼロ切片にビット次いで、whileループ行きます

ターゲットbtReadのビット長(最初のビット0が0であり、取得されたバックoutCopyの長さは、足し。)、

そして、長さはoutCopyの長さでコピーします。


公開された124元の記事 ウォンの賞賛141 ビュー160 000 +

おすすめ

転載: blog.csdn.net/weixin_36838630/article/details/79794078