为了简单起见,假设元素也是大整数。
但是数组不能包含超过Integer.MAX_值(或者更少依赖于虚拟机,请参见
this question
).
要实现这样的数据结构,一个简单的(天真的)解决方案是从LinkedList创建一个,并有一个外部BigInteger计数器。
像这样的:
class MyArray{
private final BigInteger size;
private final LinkedList<BigInteger> list;
MyArray(BigInteger size){
//ommited for simplicity
}
public BigInteger get(BigInteger index){
//ommited for simplicity
//traverse the LinkedList using a BigInteger counter and get the element
}
public void set(BigInteger index,BigInteger element){
//ommited for simplicity.
//traverse the LinkedList using a BigInteger counter and set the element
}
public BigInteger getSize(){
return size;
}
}
但是,应该可以进行一些优化。
有这种任意大小的数据结构吗?
2-数据结构最好存储在内存中,因此不考虑基于数据库的备份解决方案。例如,还可以通过将值保存在数据库中,并将元素的索引转换为字符串并用作字符串键来实现上述操作。