2011-02-01 10 views
0

可能性の重複を取得します:
Creating a byte[] from a List<Byte>バイト[]一覧から<Byte>

私はリストのリストを持っています。 startIndexからendIndexのidリストにbyte [](リストの部分配列)を取得する方法は?

+1

バイト[]またはバイト[]を意味しますか? – Joel

+0

@Joel私は重複しているとは思わない。その質問はバイト[]をバイト[]に変換することに関するものです。これは約(リ​​ストに配列)+サブリスト –

答えて

2
List<Byte> theList= new ArrayList<Byte>(); 
Byte[] your_bytes = theList.subList(startIndex,endIndex).toArray(new Byte[0]); 

最後に、あなたが(原始的)byteで作業する必要がある場合は、その後、私はあなたが確かにByte(オブジェクト)とによって得ることができ、ほとんどの場合のためにtoPrimitive utility

byte[] your_primitive_bytes = ArrayUtils.toPrimitive(your_bytes); 

のApache Commonsのコレクションをお勧めします。

+0

です。私は、 'startIndex'と' endIndex'がコードのどこかにあると仮定しています。 –

+2

OPはバイト[]を要求しましたが、それはタイプミスかもしれません。 – Joel

+0

良い点@joel私はあなたのコメントに従って私の質問を編集しました。 –

0
ArrayList<Byte> list = new ArrayList<Byte>(); 

ArrayList<Byte> subList = (ArrayList<Byte>) list.subList(fromIndex, toIndex); //(0,5) 

Byte[] array = (Byte[]) subList.toArray(); 
0

まあに、元の質問は、実際にバイト[]([]バイトではない)を含むサブリストを要求しますので、ここに行く:

List<Byte> byteList = .... some pre-populated list 
    int start = 5; 
    int end = 10; 
    byte[] bytes = new byte[end-start]; // OP explicitly asks for byte[] (unless it's a typo) 
    for (int i = start; i < end; i++) { 
     bytes[i-start] = byteList.get(i).byteValue(); 
    } 
0

あなたがbyte[]が必要な場合:

byte[] byteArray = ArrayUtils.toPrimitive(list.subList(startIndex, endIndex).toArray(new Byte[0])); 

ArrayUtils.toPrimitive