2013-05-20 216 views
-2

它是如何能夠存儲數據量(字節)輸入這樣的: 輸入數據(字節):01 23 45和67 89 10的ArrayList在ArrayList的

在最後的ArrayList行:[[01,23 ,45],[67,89,10]]

// declaration 
List<List<Byte>> row = new ArrayList<List<Byte>>(); 
List<Byte> myBytes = new ArrayList<Byte>(); 
int j=0; 

// code before this defines the arraylengt to cut the buffer in pieces of 3 bytes 
// and clears the Mybytes arraylist to be able to fill the buffer with new values 

if(arrayLength > 0) 
{ 

    myBytes.add(value); // fill first arraylist (buffer) with byte value (for example: 01) 

    if(arrayLength == 1) // when buffer myBytes is full (with values 01 23 45) write value to position j in the new arraylist 
    { 
     row.add(j,new ArrayList<Byte>()); 
     row.set(j,myBytes); 
     j +=j; 
    } 
    arrayLength -= 1; // for cutting the buffer in pieces of 3 bytes 
} 

謝謝你幫助我!

+0

這已經回答了http://stackoverflow.com/questions/4126272/how-do-i-implement-nested-arraylist – o0rebelious0o

回答

0

只需使用Arraylist和字節數組new ArrayList<Byte[]>();。這是生成[n] [3] - 矩陣的好方法。

0

我建議只使用大小爲[n] [3]的矩陣。你可以通過添加if來確保它保持爲一個字節,並且必須使它成爲一個int矩陣,這樣它可以擁有任意數量的行。

0

我找到了答案,使用arraylist緩衝值轉換爲字節數組,並將它們放置在其他arraylist。多謝你們 !注意:請使用myBytes.clear(); 這裏是我的代碼:

if(bool == true) 
{ 
myBytes.add(value); // buffer   
if(arrayLength == 1) 
{ 

    byte[] data = new byte[myBytes.size()]; 
    for (int i = 0; i < data.length; i++) 
    { 
    data[i] = (byte) myBytes.get(i); 
    } 

    row.add(data); 
}  
arrayLength -= 1; 
}