Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Problem in static method

Thread view: 
batsteve@libero.it - 21 Jun 2006 23:00 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.