1

I have a problem, that I want to solve, but I dont have the knowledge to do it yet.

Is there a possibility I can put any Method into the parameter list anytime?

For Example:

public void goThroughArray(anyMethod()){
for (int i = 0; i<array.length;i++){
    anyMethod();
}

public int[] copyArray(int[]arrayName2){
 arrayName1[i] = arrayName2[i];
}

//in main
goThroughArray(copyArray(int[]arrayName);

Is that possible?

2 Answers2

1

You can pass Function<T, R> as a parameter to other method in Java 8,

public void goThroughArray(Function<int[], int[]> myFunction){
   int[] resultArray = myFunction.apply(anyArray);
}
akash
  • 22,664
  • 11
  • 59
  • 87
0

Yes it is possible if you are using java 8(1.8).

You can use interface Function<T,R> to achieve your whatever thing.

From Oracle:

Type Parameters:

T - the type of the input to the function

R - the type of the result of the function

All Known Subinterfaces: UnaryOperator

Functional Interface:

This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

Learn more:

Blasanka
  • 21,001
  • 12
  • 102
  • 104