[英]array of DBObject with same keys each element
I am trying to create an array of DBObject, all of the elements have the same key with different values. 我正在尝试创建一个DBObject数组,所有元素都具有具有不同值的相同键。 what is the problem with this implementation ? 此实现有什么问题?
DBObject[] Out = new BasicDBObject[2];
out[0].put("VALUE","1");
out[0].put("PROPERTY","1");
out[1].put("VALUE","2");
out[1].put("PROPERTY","2");
First, Out
and out
are mixed up (use upper/lower case consistently) 首先, Out
和out
混合在一起(始终使用大写/小写)
Second, you need to initialize the objects in the array before you can use them: 其次,您需要先初始化数组中的对象,然后才能使用它们:
DBObject[] out = new BasicDBObject[2];
out[0] = new BasicDBObject();
out[0].put("VALUE","1");
out[0].put("PROPERTY","1");
out[1] = new BasicDBObject();
out[1].put("VALUE","2");
out[1].put("PROPERTY","2");
You are only creating array of references. 您仅在创建引用数组。 You need to create objects for it before assigning values. 您需要在分配值之前为其创建对象。
DBObject[] Out = new BasicDBObject[2];
// instantiating objects for the array
for(int i=0 ; i < Out.length ; i++){
Out[i] = new BasicDBObject();
}
Out[0].put("VALUE","1");
Out[0].put("PROPERTY","1");
Out[1].put("VALUE","2");
Out[1].put("PROPERTY","2");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.