【问题标题】:Arraylist in a ArraylistArraylist 中的 Arraylist
【发布时间】:2013-05-15 07:24:24
【问题描述】:

如何存储这样的数据(字节)输入: 输入数据(字节):01 23 45 和 67 89 10

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
}

谢谢你帮助我!!

【问题讨论】:

标签: java android arraylist byte


【解决方案1】:

我找到了答案,使用 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;
}

【讨论】:

    【解决方案2】:

    只需使用带有字节数组new ArrayList&lt;Byte[]&gt;(); 的Arraylist。这是生成 [n][3]-Matrix 的好方法。

    【讨论】:

      【解决方案3】:

      我建议只使用大小为 [n][3] 的矩阵。您可以通过添加 if 来确保它保持为一个字节,并且必须使其成为一个 int 矩阵,这样它就可以拥有任意多的行。

      【讨论】:

        猜你喜欢
        • 2012-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多