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?
I believe this was implemented in Java 1.5. The syntax allows you to call a method with a comma separated list of arguments instead of an array.
public static void main(String... args);
main("this", "is", "multiple", "strings");
is the same as:
public static void main(String[] args);
main(new String[] {"this", "is", "multiple", "strings"});
http://today.java.net/article/2004/04/13/java-tech-using-variable-arguments http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
Check out the Java Language Specification, Third Edition, Chapter 8 (Classes). Buried in there is this nugget:
If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation (ยง15.12.4.2).
Basically, the last parameter of any method call can have T.... If it has that, it is converted to T[].
So basically, what you have is a fancy way of reproducing the more traditional
String[] args