Can Java store methods in arrays?

Well, I wrote the code, and everything I did was for loops, but changed the method that I called. I tried using a for loop, so it would be a little neat (and out of curiosity to see if this can be done), but it does not compile when I do this because it does not recognize the element in the array as a method, I think. This is what I have:

String[] moveArray = {moveRight,moveDown,moveLeft,moveUp}; for (i = 0; i < 4; i++) { while (myWumpus.moveArray[i]) { myWumpus.moveArray[i]; generator.updateDisplay(); } } 

When I try to compile, I get

 not a statement myWumpus.moveArray[i](); ';' expected myWumpus.moveArray[i](); 

(It refers to the first statement in the while loop)

So, I think it could be because I am making it an array of type String? Is there a type method? Is it even possible? Any solutions are welcome :). In addition, I can get it to work using 4 cycles, so you do not need to show me this solution. Thanks!

+7
java arrays methods
source share
5 answers

You cannot store methods directly in arrays. However, you can store objects that implement the same method in different ways. For example:

 Mover[] moveArray = {new RightMover(), new DownMover() new LeftMover(), new UpMover() }; for (i = 0; i < 4; i++) { while (myWumpus.moveArray[i]) { moveArray[i].move(); generator.updateDisplay(); } } 
+14
source share

You cannot store methods in arrays in Java, because methods are not first-class objects in Java. This is the reason why some people prefer to use other languages ​​like Python, Scheme, etc.

The workaround is to create an interface that contains one method, and then create four classes that implement this interface - the MoveRight, MoveLeft, etc. classes. Then you can store instances of these classes in your array and call them the same way.

+5
source share

Yes, you can store methods in arrays using Reflection , however it is likely that in this situation you really want to use polymorphism .

As an example of polymorphism in relation to your problem - let's say you created the interface as follows:

 public interface MoveCommand { void move(); } 

Then you can create an implementation as follows:

 public class MoveLeftCommand implements MoveCommand { public void move() { System.out.println("LEFT"); } } 

etc. for other moving options. Then you can save them in MoveCommand[] or collection, for example List<MoveCommand> , and then List<MoveCommand> iterate over the array / collection, calling move () for each element, for example:

 public class Main { public static void main(String[] args) { List<MoveCommand> commands = new ArrayList<MoveCommand>(); commands.add(new MoveLeftCommand()); commands.add(new MoveRightCommand()); commands.add(new MoveLeftCommand()); for (MoveCommand command:commands) { command.move(); } } } 

Polymorphism is very powerful, and the above example is a very simple example of what is called a command pattern . Enjoy the rest of your Wumpus World implementation :)

+5
source share

You cannot call such methods. But you can use reflection:

Just change the first line in the while loop to:

 Method m = myWumps.getClass().getMethod(moveArray[i]); // if the method is void m.invoke(myWumps); 

(you will have to declare / catch a few exceptions)

But it's better to avoid reflection and use the Command pattern instead.

+4
source share

Updated answer for Java 8 and onwards-

Since the advent of lambda expressions and method references in Java 8, it is now possible to store various methods in variables. One of the main problems is that currently arrays do not support universal objects in Java , which makes storing methods in arrays less feasible. However, they can be stored in other data structures such as List .

So for some simple examples, you can write something like:

 List<Comparator<String>> stringComparators = new ArrayList<>(); Comparator<String> comp1 = (s1, s2) -> Integer.compare(s1.length(), s2.length()); stringComparators.add(comp1); 

or

 List<Consumer<String>> consumers = new ArrayList<>(); Consumer<String> consumer1 = System.out::println; consumers.add(consumer1); 

and then loop / iterate over List to get methods.

0
source share

All Articles