I'm doing a little method and i need to pass a method as a parameter, so i don't have to repeat code. So i have to use this method and the only thing that changes is the method i use inside this method. So, if i could pass a method in a parameter, it would simplify a lot my code. here's my code. can i use java's refletct?
public static void testsForLinkedLists(int potencia,
int repeticoesteste, int somarAoArray, String ficheiroExcel,
int validararray, Method pushMethod)
And i have this two methods that i want to use it as parameters
public class measuring_tests {
public static double timeToPushLinkedStack(int intendedPushes) {
final LinkedStackOfStrings measuringTimeToPush = new LinkedStackOfStrings();
final String element = "measuring_test";
int numberOfPushesDone = 0;
double totalPushTime = 0;
Stopwatch stopwatch = new Stopwatch();
while (numberOfPushesDone < intendedPushes) {
measuringTimeToPush.push(element);
numberOfPushesDone++;
}
totalPushTime = stopwatch.elapsedTime();
while (measuringTimeToPush.size > 0) {
measuringTimeToPush.pop();
}
return totalPushTime;
}
public static double timeToPopLinkedStack(int intendedPops) {
final LinkedStackOfStrings measuringTimeToPop = new LinkedStackOfStrings();
final String element = "measuring_test";
while (measuringTimeToPop.size < intendedPops) {
measuringTimeToPop.push(element);
}
double totalPopTime = 0;
Stopwatch stopwatch = new Stopwatch();
while (measuringTimeToPop.size > 0) {
measuringTimeToPop.pop();
}
totalPopTime = stopwatch.elapsedTime();
return totalPopTime;
}