Results 1 to 6 of 6

Thread: Define loop in another method

  1. #1
    Join Date
    Dec 2009
    Posts
    178

    Define loop in another method

    Hello,
    I have a loop in a method1 () that lets you scan a file and retrieve data from a given "for (Event e: file)" eg. My question is I want to know if we can put this loop into another method, method2 (Event e) and instead of integrating the loop in method1 (). I integrate method2 (e) in method1 (). I do not know this is possible or not, but I think this can be done, so if you guys have any idea about this then please post back. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    343

    Re: Define loop in another method

    Hello,
    Yes you can call a parametrized method within another method, it is even advisable to separate your application with atomic methods, to facilitate reuse. I do not see what could make you believe that it is not possible? Is this a concern for performance? If yes, performance is managed by the compiler, in theory, and compiler byte code, sun is powerful enough to optimize this kind of thing. But personally I do not feel any need of doing this in a code, but if you still wish you can use it.

  3. #3
    Join Date
    Nov 2009
    Posts
    347

    Re: Define loop in another method

    Hello,
    The following may help you, just have a look at it
    Code:
    Public void method() {
    
       List mlst = <Event> null;
       / / Get your list;
    
       traitementListe(mlst);
     
    }
     
    private void traitementListe(List mlst <Event>) {
       for(Event e: mlst) {
          trtment(e);
       }
    }
     
    private void trtment(Event e) {
       / / Here the method of processing an element in your loop
    }

  4. #4
    Join Date
    Dec 2009
    Posts
    296

    Re: Define loop in another method

    Hello,
    I am also new to java, but I would have done something like this
    Code:
    Public void method() {
    
       Event e = null
     
       this.trtment(e);
        / / processing on e
     
    }
     
    private void trtment(Event e) {
       for(Event e: file) 
         e = e;
         return e;
       
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    446

    Re: Define loop in another method

    Hello,
    This is a well defined code, you can try this
    Code:
    Public class myclass {
      private Collection <Event> e;
    
      / / The getter in your private member
      Public Collection <Event> geteve() {
        return e;
      }
     
      Public void charger(String file) {
        / / Read the file
        BufferedReader mfl = new BufferedReader(new FileReader(file));
    
        String line = null;
        while(line = mfl.readLine() != null) {
          Event eve = new Event();
          eve.setName(row.substring(1,10));
          e.add(eve);
        }
      }
     
      Public static void hand(String[] args) {
        myclass MyClass = new myclass();
        MyClass.charger("myListEvent.txt");
        
      }
    }

  6. #6
    Join Date
    Nov 2009
    Posts
    333

    Re: Define loop in another method

    Hello,
    I think you can even use the enums here to do the same thing. Have a look at the following, though I am not sure that this is what you want.
    Code:
     public static enum etyp {  
         COMPONENT_ADDED,  
       COMPONENT_REMOVED;  
    
       public static boolean cnt(etyp eventType) {  
            boolean cnt = false;  
            etyp[] vals = etyp.vals();  
       
            for (int i = 0; i < vals.length && !cnt; i++) {  
                cnt = (COMPONENT_ADDED.equals(vals[i]) || COMPONENT_REMOVED  
                         .equals(vals[i]));  
           }  
       
            return cnt;  
         }  
     }

Similar Threads

  1. Watercooling: Single loop or Dual Loop
    By Akolekar in forum Hardware Peripherals
    Replies: 3
    Last Post: 21-10-2011, 10:52 PM
  2. Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work
    By mADiRAkSHii in forum Operating Systems
    Replies: 4
    Last Post: 25-01-2011, 06:23 PM
  3. Method overriding versus method hiding in C#
    By ^MALARVIZHI^ in forum Software Development
    Replies: 4
    Last Post: 25-12-2010, 05:25 AM
  4. Is it possible to call destroy() method within init() Method?
    By Level8 in forum Software Development
    Replies: 3
    Last Post: 10-12-2009, 07:36 AM
  5. What is method overriding and method overloading in java
    By beelow in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 07:20 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •