In PHP, one can do foreach($array as $key=>$value){//codes. Is it possible to create a function in Java to do foreach(arr,function(key,value){//codes});?
I’m not very good at Java. For the function to work, it must accept all data types. Also, I’m not sure if callback functions can be used in Java.
P.S. “Not possible” is a valid answer, thanks!
Yes you can:
This work with another classes too.
UPDATE
When you traverse a List (array or linked list) the index would be the index. You will need to use an alternative integer to hold the index:
If you need to traverse a Map (key, value based structure) then you should use the method described by @LukasEder (adapting his code):