Originally Posted by
Starstreak
Try creating a Java class with a main method. Then create a method to handle the reversal and call it from main.
Well, that might serve to test the algorithm, and might be appropriate for a Java programmer learning Groovy, but for Groovy beginners, that
public class stuff and
public static void main() stuff just serve to clutter up the landscape.
Originally Posted by
Starstreak
Declare your array in main as follows: char[] band = { 'I', 'N', 'X', 'S' };
OK for Java; won't work in Groovy. (Have you tried it?)
Originally Posted by
Starstreak
(Bear in mind that a char [] uses single quotes ' ' and not " " <-- those are for Strings.)
In Groovy, there are no character literals. Single quotes indicate String literals. Double quotes also indicate String literals. Strings with length = 1 are "coerced" into character constants. So your second example works in Groovy with single quotes or double quotes around the initialization array members. The code from the OP was missing the "new" and that's the only thing about his array declaration/initialization that needed fixing. I'll give the Groovy alternative that looks "almost" like your first example in a minute.
Originally Posted by
Starstreak
Ideally you would need a loop to print the result to see if it is correct.
Why would you say that you need a loop to print an array of char?
In Groovy you don't need a loop. You don't even need System.out. You don't even need parentheses. Heck, you don't even need a semicolon
// Groovy print statement to print out the contents of an array of char looking like a String
println "band = " + band
I mean, you can put in all of that Java-looking stuff if you want to (as I showed in my example of debugging print statements). Many Java constructs and much Java syntax is "forward-compatible" with Groovy. (And that's not a coincidence.)
Semicolons at the end of statements are optional in Groovy. People with previous experience in languages like C and C++ and Java tend to put them there by force of habit, but they serve no purpose in Groovy itself.
Here's a complete Groovy program except I deleted the part in the function that does the work that assignment expects:
// Groovy program demonstrating two ways to reverse
// the elements of an array of char.
// First way uses List reverse method.
// Second way uses method defined in this program.
//
// Zaphod_b
//
// Note array initialization syntax differs from Java!
//
// Note that arrays of char get printed as if they
// contained a String.
char [] band = ["I", "N", "X", "S"]
println "Original : band = " + band
// Not what the assignment asked for, but I'll show
// it just for the heck of it. Don't turn this in!
// It is not what your instructor wants to see.
//
band = Arrays.asList(band).reverse()
println "After List.reverse(): band = " + band
// Without any extra class stuff, it uses the locally-defined
// function.
//
// If you wanted to emphasize the local-ness, you
// could write it like the following:
// band = this.reverse(band)
//
band = reverse(band)
println "After this.reverse(): band = " + band
//
// Locally-defined function to reverse elements in an array of chars
//
def char [] reverse (char [] favband) {
//
char [] whatever// Put your stuff here...
// Put your stuff here
// Put your stuff here
return whatever
}
Output:
Original : band = INXS
After List.reverse(): band = SXNI
After this.reverse(): band = INXS
Maybe that can be used as a starting point and a testbed.
Bottom line: Although Groovy accepts a lot of Java-looking syntax and some of it even works the way that Java programmers expect, Groovy != Java (If your grandad knows Java, and you show him Groovy, he may even say that it's "Groovy, man!" like we used to say about cool stuff in the 1960's.)
To the Original Poster: Sometimes the first one is the hardest. Once you get the program to compile and print some output, then, if the output is wrong, debugging becomes a "snap." The hard part is when it won't compile or it compiles but crashes before printing anything useful. Debugging "nothing" is harder than debugging "somthing."
Cheers!
Z