我有一个记录商店,其中包含我目前正在序列化数据的购物项目列表,如下所示
** (inside) ItemClass **
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
DataOutputStream dataOutputStream = new DataOutputStream(byteOutputStream);
dataOutputStream.writeUTF(getOwner());
dataOutputStream.writeUTF(getItemName());
dataOutputStream.writeInt(getQuantity());
dataOutputStream.close();
return byteOutputStream.toByteArray();
然后像这样将该字节数组写入记录存储(对每个 id 使用 for 循环)
for (int i = 1; i <= shoppingListStore.getNumRecords(); i++)
{
byte[] itemRecord = shoppingListStore.getRecord(i);
newItemObject.fromByteArray(itemRecord);
userShoppingList.append(newItemObject.getItemName() + " " + newItemObject.getQuantity() + " " + newItemObject.getOwner(), null);
}
问题来自使用 ID 遍历记录存储,因为这可能会因删除等而改变,因此我不断收到无效的 id 被抛出。我知道我不能使用 For each ItemObj : Recordstore 那么我如何遍历记录存储而不必担心 id 呢?