public List<DBObject> findByDateDescending(int limit) {
List<BasicDBObject> posts = null;
// XXX HW 3.2, Work Here
// Return a list of DBObjects, each one a post from the posts collection
BasicDBObject query = new BasicDBObject();
BasicDBObject sortPredicate = new BasicDBObject();
sortPredicate.put("date", -1);
DBCursor cur = postsCollection.find(query).sort(sortPredicate);
int i = 0;
while( cur.hasNext() && i < limit ) {
if ( posts == null)
posts = new ArrayList<BasicDBObject>();
DBObject obj = cur.next();
posts.add(obj);
System.out.println("findByDateDescending(" + limit + " blog entry " + obj);
i++;
}
return posts;
}
我在帖子和obj收到错误,错误是:
add(com.mongodb.BasicDBObject)List cannot be applied to com.mongodb.DBObject.
任何人都可以帮我吗?
答案 0 :(得分:1)
您正在尝试将DBObject
分配给BasicDBObject
。基本上将泛型类型分配给特定类型。这是不允许的。将您的列表更改为List<DBObject>
或obj
为BasicDBObject