Android 在不重复的情况下获取随机无间隔范围
全部 我需要有逻辑的帮助。我在数据库中有100行,随机获得所有行,没有重复,我使用了下面的代码Android 在不重复的情况下获取随机无间隔范围,android,sql,sqlite,random,Android,Sql,Sqlite,Random,全部 我需要有逻辑的帮助。我在数据库中有100行,随机获得所有行,没有重复,我使用了下面的代码 public ArrayList<Rows> getRows(){ ArrayList<Rows> myRaw=new ArrayList<Rows>(); Cursor rawCursor=this.db.query(DB_TABLE, null, null, null, null, null, null); if(rawCursor.ge
public ArrayList<Rows> getRows(){
ArrayList<Rows> myRaw=new ArrayList<Rows>();
Cursor rawCursor=this.db.query(DB_TABLE, null, null, null, null, null, null);
if(rawCursor.getCount()>0){
Random rng = new Random();
rawCursor.moveToFirst();
do {
// Ideally just create one instance globally
ArrayList<Rows> generated = new ArrayList<Raws>();
for (int i = 0; i < 371; i++)
{
while(true)
{
Integer next = rng.nextInt(371) + 1;
rawCursor.moveToPosition(next);
Rows raw=new Raws(rawCursor.getInt(rawCursor.getColumnIndex("_id")),rawCursor.getString(rawCursor.getColumnIndex("raw")),rawCursor.getInt(rawCursor.getColumnIndex("fav")));
if (!generated.contains(raw))
{
// Done for this iteration
generated.add(raw);
break;
}
}
}
myRaw=generated;
} while (rawCursor.moveToNext());
rawCursor.close();
}
return myRaw;
}
public ArrayList getRows(){
ArrayList myRaw=新的ArrayList();
Cursor rawCursor=this.db.query(db_表,null,null,null,null,null);
if(rawCursor.getCount()>0){
随机rng=新随机();
rawCursor.moveToFirst();
做{
//理想情况下,只需全局创建一个实例
生成的ArrayList=新的ArrayList();
对于(int i=0;i<371;i++)
{
while(true)
{
整数next=rng.nextInt(371)+1;
rawCursor.moveToPosition(下一步);
Rows raw=新的raw(rawCursor.getInt(rawCursor.getColumnIndex(“_id”)、rawCursor.getString(rawCursor.getColumnIndex(“raw”)、rawCursor.getInt(rawCursor.getColumnIndex(“fav”));
如果(!generated.contains(原始))
{
//完成此迭代
生成。添加(原始);
打破
}
}
}
myRaw=已生成;
}while(rawCursor.moveToNext());
rawCursor.close();
}
返回myRaw;
}
谢谢大家您可以告诉数据库按以下顺序排列记录:
您可以告诉数据库按以下方式对记录进行排序:
洗牌怎么样?
洗牌怎么样?
你的问题是什么?i、 e.你写的代码有什么问题吗?有没有理由不使用随机排序()?@RenatoLochetti:好的,但当我问问题,但我没有得到正确的答案时,我怎么能接受它,因为其他开发人员可能会遵循它。@Mattwipple你能解释更多关于随机排序的内容吗?如果你添加一个随机排序()子句添加到原始查询中,结果集将随机化,您可以对其进行迭代。您将把责任转移到SQLite,它将进行超出您能力范围的内部优化。公认的答案解决了这个问题。你的问题是什么?i、 e.你写的代码有什么问题吗?有没有理由不使用随机排序()?@RenatoLochetti:好的,但当我问问题,但我没有得到正确的答案时,我怎么能接受它,因为其他开发人员可能会遵循它。@Mattwipple你能解释更多关于随机排序的内容吗?如果你添加一个随机排序()子句添加到原始查询中,结果集将随机化,您可以对其进行迭代。您将把责任转移到SQLite,它将进行超出您能力范围的内部优化。公认的答案解决了这个问题。
cursor = db.query(DB_TABLE, null, null, null, null, null, "random()");
public ArrayList<Rows> getRows() {
ArrayList<Rows> myRaw = new ArrayList<Rows>();
Cursor rawCursor = this.db.query(DB_TABLE, null, null, null, null, null, null);
if (rawCursor.getCount() > 0) {
// get all the rows
rawCursor.moveToFirst();
do {
Rows raw = new Raws(rawCursor.getInt(rawCursor.getColumnIndex("_id")), rawCursor.getString(rawCursor.getColumnIndex("raw")), rawCursor.getInt(rawCursor.getColumnIndex("fav")));
myRaw.add(raw);
} while (rawCursor.moveToNext());
rawCursor.close();
}
// shuffle the rows in some kind of random order
Collections.shuffle(myRaw);
return myRaw;
}
//Random with seed;
Random r = new Random(68498493);
Collections.shuffle(myRaw, r);