Use generics.
public <T>void foo(T[] array) {
System.out.println(array.length);
}
This will not work for array of primitive types, such as int[], boolean[], double[],... You have to use their class wrappers instead: Integer[], Boolean[], Double[], ... or overload your method for each needed primitive type separately.
If you are curious you may have a look at a analogical problem (generics and classes vs. primitive types) with Streams here: https://stackoverflow.com/a/23010472/2886891
java - How can I make a method take an array of any type as a parameter? - Stack Overflow
Java - Creating an array of methods - Stack Overflow
Array of function pointers in Java - Stack Overflow
Using an array as the parameter for a method in Java - Stack Overflow
Videos
Use generics.
public <T>void foo(T[] array) {
System.out.println(array.length);
}
This will not work for array of primitive types, such as int[], boolean[], double[],... You have to use their class wrappers instead: Integer[], Boolean[], Double[], ... or overload your method for each needed primitive type separately.
If you are curious you may have a look at a analogical problem (generics and classes vs. primitive types) with Streams here: https://stackoverflow.com/a/23010472/2886891
Well, you can do something like this (because an array is an Object) -
public static int getArrayLength(Object array) {
return Array.getLength(array);
}
public static void main(String[] args) {
int[] intArray = { 1, 2, 3 };
String[] stringArray = { "1", "2", "c", "d" };
System.out.println(getArrayLength(intArray));
System.out.println(getArrayLength(stringArray));
}
Output is
3
4
Whenever you think of pointer-to-function, you translate to Java by using the Adapter pattern (or a variation). It would be something like this:
public class Node {
...
public void goNorth() { ... }
public void goSouth() { ... }
public void goEast() { ... }
public void goWest() { ... }
interface MoveAction {
void move();
}
private MoveAction[] moveActions = new MoveAction[] {
new MoveAction() { public void move() { goNorth(); } },
new MoveAction() { public void move() { goSouth(); } },
new MoveAction() { public void move() { goEast(); } },
new MoveAction() { public void move() { goWest(); } },
};
public void move(int index) {
moveActions[index].move();
}
}
Just have your nodes be objects that all adhere to the same interface, then you'll be able to call their methods reliably.
Java doesn't have a function pointer per se (or "delegate" in C# parlance). This sort of thing tends to be done with anonymous subclasses.
public interface Worker {
void work();
}
class A {
void foo() { System.out.println("A"); }
}
class B {
void bar() { System.out.println("B"); }
}
A a = new A();
B b = new B();
Worker[] workers = new Worker[] {
new Worker() { public void work() { a.foo(); } },
new Worker() { public void work() { b.bar(); } }
};
for (Worker worker : workers) {
worker.work();
}
You can achieve the same result with the functor pattern. For instance, having an abstract class:
abstract class Functor
{
public abstract void execute();
}
Your "functions" would be in fact the execute method in the derived classes. Then you create an array of functors and populate it with the apropriated derived classes:
class DoSomething extends Functor
{
public void execute()
{
System.out.println("blah blah blah");
}
}
Functor [] myArray = new Functor[10];
myArray[5] = new DoSomething();
And then you can invoke:
myArray[5].execute();
I have a method with the following signature - public static void max(int tt []). I can properly initialize an array by doing this - int [] a = {4242343,23,423,423,4};. However, java gives me an array if I try passing an array to a method like this - max( {43,43,34 });. Is there a reason for this?