Search code examples
androidrecursionarraysrandom-access

continue searching for random string from array avoiding duplicates and StackOverflowError


I use such a mechanism in my application: get random string from string array and put it in the SQLite database. Then I send this new string to the widget and to ArrayList for ListActivity representation. Before the putting new object to the database I need to check if it is no object with the same text field value. My Quote object:

    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;
            }

}

and this is how I get my random string with checking mechanism:

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;
  }

but when I do it like this, I've got StackOverflowError. How can I solve it?


Solution

  • I solved this by Singletone pattern using.

    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;
    
    }
    }