Java 不使用数据库存储表数据的最佳方法?
我需要存储解析的Java 不使用数据库存储表数据的最佳方法?,java,android,data-structures,Java,Android,Data Structures,我需要存储解析的XML中的数据,我对解析并没有问题,我可以处理。问题是我收到的是一个表数据,基本上每次的列名、列号和数据本身都是不同的。现在我不想使用SQLite数据库,因为我有少量的条目,我看不出它适合这种情况。我甚至认为我不能使用它,因为我必须为接收到的每个数据集创建一个新表。下面是一些更清楚的例子: <Details1_Collection> <Details1 ProductName="Bicycle" Units="2341&q
XML
中的数据,我对解析并没有问题,我可以处理。问题是我收到的是一个表数据,基本上每次的列名、列号和数据本身都是不同的。现在我不想使用SQLite
数据库,因为我有少量的条目,我看不出它适合这种情况。我甚至认为我不能使用它,因为我必须为接收到的每个数据集创建一个新表。下面是一些更清楚的例子:
<Details1_Collection>
<Details1 ProductName="Bicycle" Units="2341" DefectedUnits="125" />
<Details1 ProductName="Television" Units="3154" DefectedUnits="75" />
<Details1 ProductName="Keyboard" Units="2413" DefectedUnits="12" />
...
</Details1_Collection>
现在您可以注意到XML标记是不同的:Details1\u Collection
和Details\u Collection
,Details1
和Details
我几乎每次都会收到一个新的标签集。但这对我来说不是问题,因为我会在一个更快的服务器调用中收到它们,所以我知道应该解析哪些标记。我不需要存储属性以及名称,因为我已经有了它们。我只需要存储值
问题:
我应该如何存储属性数据,它基本上是一个表数据,但它是一个不同的表,每次都有不同的列名和编号?我不希望将此数据作为String/Dom
元素存储在其父对象中,而是希望将其解析为一些java
可以在UI线程上轻松访问的数据结构
简而言之:
这些数据是更大对象的一部分,我的目标是将其解析为某种数据结构,并将其作为父类的一部分附加
如果有任何帮助,我们将不胜感激。我无意中发现了以下论坛帖子: ArrayList中有一个ArrayList的实现,代码如下:
import java.util.ArrayList;
public class ArrayList2d<Type>
{
ArrayList<ArrayList<Type>> array;
public ArrayList2d()
{
array = new ArrayList<ArrayList<Type>>();
}
/**
* ensures a minimum capacity of num rows. Note that this does not guarantee
* that there are that many rows.
*
* @param num
*/
public void ensureCapacity(int num)
{
array.ensureCapacity(num);
}
/**
* Ensures that the given row has at least the given capacity. Note that
* this method will also ensure that getNumRows() >= row
*
* @param row
* @param num
*/
public void ensureCapacity(int row, int num)
{
ensureCapacity(row);
while (row < getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).ensureCapacity(num);
}
/**
* Adds an item at the end of the specified row. This will guarantee that at least row rows exist.
*/
public void Add(Type data, int row)
{
ensureCapacity(row);
while(row >= getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).add(data);
}
public Type get(int row, int col)
{
return array.get(row).get(col);
}
public void set(int row, int col, Type data)
{
array.get(row).set(col,data);
}
public void remove(int row, int col)
{
array.get(row).remove(col);
}
public boolean contains(Type data)
{
for (int i = 0; i < array.size(); i++)
{
if (array.get(i).contains(data))
{
return true;
}
}
return false;
}
public int getNumRows()
{
return array.size();
}
public int getNumCols(int row)
{
return array.get(row).size();
}
}
import java.util.ArrayList;
公共类ArrayList2d
{
数组列表数组;
公共ArrayList2d()
{
数组=新的ArrayList();
}
/**
*确保最小容量为num行。请注意,这并不保证
*有那么多行。
*
*@param num
*/
公共空间容量(整数)
{
array.ensureCapacity(num);
}
/**
*确保给定行至少具有给定的容量。请注意
*此方法还将确保getNumRows()>=行
*
*@param行
*@param num
*/
公共空间容量(int行,int num)
{
保证能力(世界其他地区);
while(行=getNumRows())
{
add(新的ArrayList());
}
获取(行)、添加(数据);
}
公共类型get(int行,int列)
{
返回数组.get(行).get(列);
}
公共无效集(整数行、整数列、类型数据)
{
数组.get(行).set(列,数据);
}
公共无效删除(整数行,整数列)
{
数组。获取(行)。删除(列);
}
公共布尔包含(类型数据)
{
对于(int i=0;i
这是我目前找到的存储我想要存储的数据的最佳方式。
你认为呢,Java专家
谢谢。有什么原因不能执行典型的Java序列化吗?与中一样,创建表示数据的类,让它们实现可序列化的
code>,然后使用object(Input | Output)流将整个对象图读写到文件中
请记住,这只有在整个对象图都能放入内存时才有效。您可以使用XML格式存储属性,因为您的应用程序中已经有了XML解析器。我可以这样做,但最终我必须将这些数据解析为某种java对象,以便在UI线程上访问它。因为解析是在后台使用AsyncTask执行的。为什么不将解析方法设为接口,并以两种方式实现接口,即在UI上解析和在UI上解析?java语言中是否真的有任何数据结构可以存储这种类型的数据?我不需要存储属性名称,因为我已经有这些。我只需要存储值。你有没有一个代码示例来说明它是如何实现的?我从未执行过典型的Java序列化。这意味着表示我的数据的类应该在运行时创建。我不知道是怎么做到的。哥们,谷歌搜索java序列化教程
第二次成功。对不起,我只是不知道该找什么。我会调查的。谢谢你的帮助。
import java.util.ArrayList;
public class ArrayList2d<Type>
{
ArrayList<ArrayList<Type>> array;
public ArrayList2d()
{
array = new ArrayList<ArrayList<Type>>();
}
/**
* ensures a minimum capacity of num rows. Note that this does not guarantee
* that there are that many rows.
*
* @param num
*/
public void ensureCapacity(int num)
{
array.ensureCapacity(num);
}
/**
* Ensures that the given row has at least the given capacity. Note that
* this method will also ensure that getNumRows() >= row
*
* @param row
* @param num
*/
public void ensureCapacity(int row, int num)
{
ensureCapacity(row);
while (row < getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).ensureCapacity(num);
}
/**
* Adds an item at the end of the specified row. This will guarantee that at least row rows exist.
*/
public void Add(Type data, int row)
{
ensureCapacity(row);
while(row >= getNumRows())
{
array.add(new ArrayList<Type>());
}
array.get(row).add(data);
}
public Type get(int row, int col)
{
return array.get(row).get(col);
}
public void set(int row, int col, Type data)
{
array.get(row).set(col,data);
}
public void remove(int row, int col)
{
array.get(row).remove(col);
}
public boolean contains(Type data)
{
for (int i = 0; i < array.size(); i++)
{
if (array.get(i).contains(data))
{
return true;
}
}
return false;
}
public int getNumRows()
{
return array.size();
}
public int getNumCols(int row)
{
return array.get(row).size();
}
}