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.
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.
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
}
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;
}
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");
}
}
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;
}
}