I have this program that builds and writes anagrams in a static method.
public class Anagramma {
static int livelloRicorsione=-1;
public Anagramma() {
}
public void anagrammaParola(String parola){
StringBuffer a=new StringBuffer(parola);
StringBuffer b=new StringBuffer(parola);
calcolaAnag(a,b);
}
public static void calcolaAnag(StringBuffer temp, StringBuffer anag){
int i, j;
StringBuffer subTemp;
livelloRicorsione++;
if (temp.length() == 1) {
anag.setCharAt(livelloRicorsione, temp.charAt(0));
System.out.println(anag);
}
else
for (i = 0; i < temp.length(); i++) {
anag.setCharAt(livelloRicorsione, temp.charAt(i));
subTemp = new StringBuffer();
for (j = 0; j < temp.length(); j++)
if (j != i)
subTemp.insert(subTemp.length(), temp.charAt(j));
calcolaAnag(subTemp, anag);
}
livelloRicorsione--;
}
}
I don't want to write the anagrams, but I want to record in a list (or
vector or etc.), and to use the list of words out of this Class, but if
I use a list in a static method it must be static and so it records
only the last word. How can I do?
Thank for your help.
Stefano Buscherini
Moiristo - 21 Jun 2006 23:35 GMT
> I have this program that builds and writes anagrams in a static method.
<code>
> I don't want to write the anagrams, but I want to record in a list (or
> vector or etc.), and to use the list of words out of this Class, but if
> I use a list in a static method it must be static and so it records
> only the last word. How can I do?
Can you show me the code where you save the results in the list? Anyway,
I think it should look like:
public static java.util.ArrayList results;
public static void anagrammaParola(String parola, Arraylist ar){
StringBuffer a=new StringBuffer(parola);
StringBuffer b=new StringBuffer(parola);
results = ar;
calcolaAnag(a,b);
}
public static void main(String[] args){
ArrayList test = new ArrayList();
anagrammaParola("Italy",test);
anagrammaParola("!=",test);
anagrammaParola("Weltmeister",test);
foreach(String s : test) System.out.println(s);
}
Hendrik Maryns - 22 Jun 2006 09:13 GMT
> public static void main(String[] args){
> ArrayList test = new ArrayList();
[quoted text clipped - 4 lines]
> foreach(String s : test) System.out.println(s);
> }
LOL!
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html