I’m new to Java and wondering how I could go about implementing a method like Ruby’s .each to make iterating over classes like Arrays and Lists easier?
I’m new to Java and wondering how I could go about implementing a method
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You can’t make an each function, like Ruby has, since Java doesn’t accept blocks as parameters like Ruby does. You could use an interface and provide an anonymous class definition, but that’s way more complicated than just using Java’s for each construct. To do for each in Java, you can do something like the following: