It would be extremely strange to do what you are trying to do. If you are looking for a universal way to do this, you will need to delve into the reflection (java.lang.reflect). If you really have an array / collection with 1, 2, or 3 int and you want to call another method based on this, then just write a method that calculates the number of values ββin the βsubstanceβ and calls the appropriate method.
Can you tell us why you want to do this?
Edit:
Code for hard coding:
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 errors with printStackTrace, but this is the 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 - this one will work for any method (you pass it the method name). This is the least secure option on the bundle - 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 + ")"); } }