Android 如何避免随机字符串重复出现

Android 如何避免随机字符串重复出现,android,random,repeat,arrays,Android,Random,Repeat,Arrays,我喜欢在文本视图中随机显示字符串数组中的一些字符串。我正在使用下面的代码,但问题是,大多数时候某些字符串会重复显示。我想要的是,一旦显示的字符串就不应该再显示。我花了几个小时搜索代码,但这些代码对我都不起作用。请帮忙。提前谢谢 public void GetQuotes(View view) { Resources res = getResources(); myString = res.getStringArray(R.array.Array);

我喜欢在文本视图中随机显示字符串数组中的一些字符串。我正在使用下面的代码,但问题是,大多数时候某些字符串会重复显示。我想要的是,一旦显示的字符串就不应该再显示。我花了几个小时搜索代码,但这些代码对我都不起作用。请帮忙。提前谢谢

public void GetQuotes(View view) {
     Resources res = getResources();
            myString = res.getStringArray(R.array.Array);
            String q = myString[rgenerator.nextInt(myString.length)];                               
            TextView tv = (TextView)findViewById(R.id.textView1);               
            tv.setText(q);  

我建议手动检查它以前是否使用过,或者使用一个集合,然后在该集合中写入字符串


通常,数组和列表不是为了避免重复而设计的,它们是作为一种集合类型设计的,可以维护许多元素的顺序。如果需要更适合作业的集合,则需要集合:

 Set<String> set = new HashSet<String>();
Set Set=newhashset();
避免重复。

这里有一个非常简单的解决方案。 现在,当我开玩笑地说这句话的时候,如果你想要一个简单的解决方案,你可以有一个专用的字符串变量来存储最后使用的问题。然后,如果您将其初始化为空字符串,它将变得非常简单。假设变量被称为last

String q = myString[rgenerator.nextInt(myString.length)]; 
//q has a string which may or may not be the same as the last one
//the loop will go on until this q is different than the last
//and it will not execute at all if q and last are already different
while (last.equals(q))
{
    //since q and last are the same, find another string
    String q = myString[rgenerator.nextInt(myString.length)]; 
};
//this q becomes last for the next time around
last = q;
现在,在其他一些问题中,需要记住的一个关键问题是,这只能确保q[1]不能跟在q[1]后面,但它并不能完全避免一个场景,荒谬的是,比如q[1]、q[2]、q[1]、q[2]等等

这里有一个ArrayList,它同样简单。
List list1=new ArrayList();
List list2=新的ArrayList();
for(int i=0;i
Java内置了数组洗牌方法,将所有项目放入一个列表中,随机洗牌,然后获取第一个元素,直到有元素为止。如果为空,请再次添加所有元素,然后再次洗牌:

private List<String> myString;

public void GetQuotes(View view) {
    Resources res = getResources();
    if (myString==null || myString.size()==0) {
        myString = new ArrayList<String>();
        Collections.addAll(myString, res.getStringArray(R.array.Array));
        Collections.shuffle(myString); //randomize the list
    }
    String q = myString.remove(0);
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText(q);
}
私有列表myString;
公共void GetQuotes(视图){
Resources res=getResources();
if(myString==null | | myString.size()==0){
myString=newarraylist();
addAll(myString,res.getStringArray(R.array.array));
Collections.shuffle(myString);//随机化列表
}
字符串q=myString.remove(0);
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(q);
}

Ah,好的ol'伪随机。这个怎么样?创建两个字符串列表。使用列表1上的随机变量提取字符串。将其从列表1中删除并放入列表2中。你的下一个随机数肯定和上一个不一样。从列表1中提取完最后一个字符串后,将列表2中的所有字符串放回列表1,然后清空列表2,然后重新开始?我不知道怎么做。如果你想雇佣一名程序员,你能推荐一种不那么复杂的方法吗。Gauravs答案向您展示了如何创建集合。如果你看的话,一定会有很多关于如何使用电视机的教程。谢谢。我是安卓系统的初学者,学习很多东西。我将尝试设置。谢谢。我将尝试并回复您我的模拟器在Win7中经常崩溃。所以我为Ubuntu下载了它。现在好了。我已经尝试了你的sol,没有错误,但在运行时,应用程序崩溃。但幸运的是Daniel Fekete的解决方案对我有效。非常感谢您使用linux+1。而且,我很高兴他的解决方案为你工作。请选择他的答案。
private List<String> myString;

public void GetQuotes(View view) {
    Resources res = getResources();
    if (myString==null || myString.size()==0) {
        myString = new ArrayList<String>();
        Collections.addAll(myString, res.getStringArray(R.array.Array));
        Collections.shuffle(myString); //randomize the list
    }
    String q = myString.remove(0);
    TextView tv = (TextView)findViewById(R.id.textView1);
    tv.setText(q);
}