It would be extremely odd to do what you are trying to do. If you are looking for a general purpose way of doing it you would need to dig into reflection (java.lang.reflect). If you really have an array/collection with 1, 2, or 3 ints and want to call a different method based on that then just write a method that figures out the number of values in the "thing" and calls the appropriate method.
Can you tell us why you want to do this?
Edit:
Code for the hard coded way:
public class Main
{
public static void main(String[] args)
{
final Main main;
main = new Main();
main.callFoo(new int[] {1});
main.callFoo(new int[] {1, 2});
main.callFoo(new int[] {1, 2, 3});
}
private void callFoo(final int[] values)
{
if(values.length == 1)
{
foo(values[0]);
}
else if(values.length == 2)
{
foo(values[0], values[1]);
}
else if(values.length == 3)
{
foo(values[0], values[1], values[2]);
}
else
{
throw new Error("too many values: " + values.length);
}
}
private void foo(int a)
{
System.out.println("foo(" + a + ")");
}
private void foo(int a, int b)
{
System.out.println("foo(" + a + ", " + b + ")");
}
private void foo(int a, int b, int c)
{
System.out.println("foo(" + a + ", " + b + ", " + c + ")");
}
}
Here is the reflection version (I would not handle the errors via printStackTrace, but it is a starting point):
public class Main
{
public static void main(String[] args)
{
final Main main;
main = new Main();
main.callFoo(new int[] {1});
main.callFoo(new int[] {1, 2});
main.callFoo(new int[] {1, 2, 3});
}
private void callFoo(final int[] values)
{
final Class[] parameters;
parameters = new Class[values.length];
for(int i = 0; i < parameters.length; i++)
{
parameters[i] = int.class;
}
try
{
final Method method;
final Object[] args;
method = Main.class.getDeclaredMethod("foo", parameters);
args = new Object[values.length];
for(int i = 0; i < args.length; i++)
{
args[i] = Integer.valueOf(values[i]);
}
method.invoke(this, args);
}
catch(final IllegalAccessException ex)
{
ex.printStackTrace();
}
catch(final IllegalArgumentException ex)
{
ex.printStackTrace();
}
catch(final InvocationTargetException ex)
{
ex.printStackTrace();
}
catch(final NoSuchMethodException ex)
{
ex.printStackTrace();
}
catch(final SecurityException ex)
{
ex.printStackTrace();
}
}
private void foo(int a)
{
System.out.println("foo(" + a + ")");
}
private void foo(int a, int b)
{
System.out.println("foo(" + a + ", " + b + ")");
}
private void foo(int a, int b, int c)
{
System.out.println("foo(" + a + ", " + b + ", " + c + ")");
}
}
Edit... last one - this one will work for any method (you pass it the name of the method). This is the least safe one on the bunch - a typo in the name can ruin your day :-)
public class Main
{
public static void main(String[] args)
{
final Main main;
main = new Main();
main.call("foo", new int[] {1});
main.call("foo", new int[] {1, 2});
main.call("foo", new int[] {1, 2, 3});
main.call("bar", new int[] {1});
main.call("bar", new int[] {1, 2});
main.call("bar", new int[] {1, 2, 3});
}
private void call(final String methodName,
final int[] values)
{
final Class[] parameters;
parameters = new Class[values.length];
for(int i = 0; i < parameters.length; i++)
{
parameters[i] = int.class;
}
try
{
final Method method;
final Object[] args;
method = Main.class.getDeclaredMethod(methodName, parameters);
args = new Object[values.length];
for(int i = 0; i < args.length; i++)
{
args[i] = Integer.valueOf(values[i]);
}
method.invoke(this, args);
}
catch(final IllegalAccessException ex)
{
ex.printStackTrace();
}
catch(final IllegalArgumentException ex)
{
ex.printStackTrace();
}
catch(final InvocationTargetException ex)
{
ex.printStackTrace();
}
catch(final NoSuchMethodException ex)
{
ex.printStackTrace();
}
catch(final SecurityException ex)
{
ex.printStackTrace();
}
}
private void foo(int a)
{
System.out.println("foo(" + a + ")");
}
private void foo(int a, int b)
{
System.out.println("foo(" + a + ", " + b + ")");
}
private void foo(int a, int b, int c)
{
System.out.println("foo(" + a + ", " + b + ", " + c + ")");
}
private void bar(int a)
{
System.out.println("bar(" + a + ")");
}
private void bar(int a, int b)
{
System.out.println("bar(" + a + ", " + b + ")");
}
private void bar(int a, int b, int c)
{
System.out.println("bar(" + a + ", " + b + ", " + c + ")");
}
}