我已经向MongoDB插入了一个short []数组。这很容易。现在我试图检索相同的数组。它返回一个BasicDBList。我想对列表的元素进行一些操作。因此,我想将其转换为 Java short []返回。我怎样才能做到这一点 ?
以下是代码:
/ *插入1DArray * /
DB dB = (new MongoClient("localhost",27017)).getDB("Test1DArray");
DBCollection dbcollection = dB.getCollection("Test1DArray");
BasicDBObject aisDocument = new BasicDBObject();
aisDocument.append("TDArray",out1D);
dbcollection.insert(aisDocument);
/ *获取1DArray * /
ObjectId MLMatrixObjectsID = (ObjectId)aisDocument.get( "_id" );
System.out.println(MLMatrixObjectsID);
BasicDBObject fields = new BasicDBObject();
fields.put("_id", MLMatrixObjectsID);
DBCollection dbcollectionfetch = dB.getCollection("Test1DArray");
DBCursor cursor = dbcollectionfetch.find(fields);
BasicDBList ODarr=null;
while (cursor.hasNext()) {
ODarr = (BasicDBList)(cursor.next().get("TDArray"));
for(int cell=0; cell < ODarr.size(); cell++){
System.out.println(ODarr.get(cell));
}
}
感谢任何帮助。谢谢。
答案 0 :(得分:0)
Object[] str = `ODarr.toArray();
short[] x = new short[str.length];
for (int i = 0; i < str.length; i++) {
x[i]=((Integer) str[i]).shortValue();}
这有帮助吗?