|
| |||||||||
| Tags: do while, for, function, loop, looping statements, method, while |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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
| |||
| |||
| 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;
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Define loop in another method" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Watercooling: Single loop or Dual Loop | Akolekar | Hardware Peripherals | 3 | 21-10-2011 11:52 PM |
| Reboot Loop in Windows 7 Reboot loop and Safe mode doesn't work | mADiRAkSHii | Operating Systems | 4 | 25-01-2011 07:23 PM |
| Method overriding versus method hiding in C# | ^MALARVIZHI^ | Software Development | 4 | 25-12-2010 06:25 AM |
| Is it possible to call destroy() method within init() Method? | Level8 | Software Development | 3 | 10-12-2009 08:36 AM |
| What is method overriding and method overloading in java | beelow | Software Development | 3 | 17-11-2009 08:20 AM |