继续从数组中搜索随机字符串,避免重复和StackOverflowError

时间:2013-06-17 14:34:23

标签: android recursion arrays random-access

我在我的应用程序中使用这样的机制:从字符串数组中获取随机字符串并将其放在SQLite数据库中。然后我将这个新字符串发送到窗口小部件,并将ArrayList发送给ListActivity表示。 在将新对象放入数据库之前,我需要检查它是否是具有相同文本字段值的对象。 我的报价对象:

    public class Quote {
private String text;
private String date;
        public Quote(String text, String date)
        {
            this.text = text;
            this.date = date;
        }
        public Quote(){}
        public String getText()
        {return this.text;}
        public String getDate()
        {return this.date;}
        public void setText(String text)
        {
            this.text = text;
        }
        public void setDate(String date)
        {
            this.date = date;
        }
          @Override
            public String toString() {
                return text + "" + date;

            }

          @Override
          public boolean equals(Object obj) {
                 if (this == obj)
                     return true;
                 if (obj == null)
                     return false;
                 if (getClass() != obj.getClass())
                     return false;
                 final Quote other = (Quote) obj;
                 if (text.equalsIgnoreCase(other.text))
                     return false;
                 return true;
             }
          @Override
          public int hashCode() {
              int hash = 7;
                hash = 89 * hash + (this.text != null ? this.text.hashCode() : 0);
                hash = 89 * hash + (this.date != null ? this.date.hashCode() : 0);
                return hash;
            }

}

这就是我通过检查机制得到随机字符串的方法:

private String getRandomString(Context context)
  {
      String l  = "";
      String[] a = context.getResources().getStringArray(R.array.quotes); 
      l  = a[rgenerator.nextInt(a.length)];
      Quote quote = new Quote(l,mydate);
   ArrayList<Quote> intermediate =  getQuotes(context);
   if (intermediate.contains(quote))
    l=getRandomString(context);

     return l;
  }

但是当我这样做时,我得到了StackOverflowError。我该如何解决?

2 个答案:

答案 0 :(得分:1)

为此,我建议使用Collections.shuffle()

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#shuffle(java.util.List)

所以,比如:

String[] a = context.getResources().getStringArray(R.array.quotes); 
LinkedList<String> quotes = new LinkedList<String>(Arrays.asList(a))
Collections.shuffle(quotes);

然后你可以像这样拉出引号:

String aRandomQuote = quotes.pop();

答案 1 :(得分:0)

我用Singleton模式解决了这个问题。

public class StringSingletone {
static int i;
String []a;
private static StringSingletone instance;
LinkedList<String> quotes ;
// Private constructor prevents instantiation from other classes
private StringSingletone(Context context) {

    a = context.getResources().getStringArray(R.array.quotes);
    quotes = new LinkedList<String>(Arrays.asList(a));
    Collections.shuffle(quotes);
}

/**
 * SingletonHolder is loaded on the first execution of Singleton.getInstance() 
 * or the first access to SingletonHolder.INSTANCE, not before.
 */

public static StringSingletone getInstance() {
    return instance;
}
public static void initInstance(Context context)
  {
    if (instance == null)
    {
      // Create the instance
      instance = new StringSingletone(context);
    }
  }
private void remakeObj(Context context)
{
    a = context.getResources().getStringArray(R.array.quotes);
    quotes = new LinkedList<String>(Arrays.asList(a));
    Collections.shuffle(quotes);

}
public String getRandomString(Context context)
{
              if (quotes.size()==0)
              {
                  remakeObj( context);
              }
              String l  = quotes.pop();
             return l;

}
}