Java Convert Array to Parameters

Is there a way to convert an array to a parameter list.?

main(){ //"a" is an array or a list or some collection myPrint(a.SomeMethod); } void myPrint(int a){ //Do Stuff to arguments } void myPrint(int a, int b){ //Do Stuff to arguments } void myPrint(int a, int b, int c){ //Do Stuff to arguments } 

I want to convert "a" to a list of parameters / arguments so that it automatically calls the corresponding function.

+7
java parameters
source share
3 answers
 main(){ int[] a = {1,2,3}; MyPrint(a); } void MyPrint(int... x){ //Do Stuff to arguments (accessing them by its index) } 
+8
source share

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 + ")"); } } 
+2
source share

In short, this is truly impossible. The only way to accept a variable number of arguments is with an int...x construct. Otherwise, you need to figure out how many elements are in a , and then figure out which one to call.

 if (a.length == 2) { MyPrint(a[0], a[1]); } else if (a.length == 3) { MyPrint(a[0], a[1], a[2]); } // etc 
0
source share

All Articles