Problem in static method

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
 
M

Moiristo

I have this program that builds and writes anagrams in a static method.
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);
}
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
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);
}

LOL!

- --
Hendrik Maryns

==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFEmlE8e+7xMGD3itQRAuQcAJ9aiP8yKsUNpKml2zm6dddf2c6vPQCcCz/g
0v6QCwkAOtxCg72JmniV68M=
=mDbw
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,783
Latest member
RickeyDort

Latest Threads

Top