B
batsteve
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
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