This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Groovy and the fly likes Java-Groovy : passing arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Languages » Groovy
Bookmark "Java-Groovy : passing arrays" Watch "Java-Groovy : passing arrays" New topic
Author

Java-Groovy : passing arrays

Paul Ristevian
Greenhorn

Joined: Jun 24, 2009
Posts: 12
Hello,
From a previous post someone helped me with getting a Java class to interact with Groovy class.
I was able to pass string literals and regular variables, but having difficulties with passing arrays.

SO this is the situation: I wish to call groovy class from Java.
But when I call the groovy class in the main{} of Java, I wish to pass a Java Array.
But what is the equivalent in to Java Array in Groovy.
I saw somewhere, that you may need to do some kind of casting on a Groovy List structure.

I am using Netbeans IDE by the way.

Thanks.

P
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Since everything in Groovy is an object, I don't think it has a concept of array. So, you probably need to change your Java signature to take in java.util.List rather than array. In java, it's quite easy to convert from a List to an array.


SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Freddy:

Groovy has arrays: JN1025-Arrays.

John.
Paul Ristevian
Greenhorn

Joined: Jun 24, 2009
Posts: 12
OK, thanks for the reply.
But let me get into some code to get into specifics.

Does that mean I can do the following:

In Java we have this declaration :
**************************************
String[] aas = { "array", "of", "String", };
**************************************

In Groovy we have this class :

**************************************
class HelloWorld {
def greet( name ){
return "Hello ${name[0]} with your new ${name[1]} and your second ${name[2]}, and that is it!"
}
}
**************************************


SO in Java :: Main we have this java passing array to groovy instantiation :
HelloWorld helloArrayMethodpass = new HelloWorld();

helloArrayMethodpass.setgreet(aas);
System.out.println(helloArrayMethodpass.getgreet());

*****************************************************

Or do I have to declare the parameter 'name' in the groovy snippet of code first to some groovy array, like this :

def name= new Object[4] //we must specify the size of the fixed-size array
assert name.size() == 4

*******************************************************

Can you please let me know if any of the above is correct.


Paul


John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Paul:

You can probably just do this:



John.
Paul Ristevian
Greenhorn

Joined: Jun 24, 2009
Posts: 12
Hi thanks I will give that a try.
it is good if it this simple.

By the way, I am not that familiar with the 'assert' statement in Groovy.
And have difficulty finding the definition of this 'assert'. People just use it
and hardly explain , i guess, this is due to the fact that it is considered very basic,
and due to this it is not explained much out on the web sites/groovy tutorial sites.

If you can give me the explanation it will be much appreciated.

Sincerely,

Paul
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Paul:

You can check out the Java assertions tutorial here. Alternatively, you can use the JUnit or TestNG assert classes to do the same thing.

John.
Paul Ristevian
Greenhorn

Joined: Jun 24, 2009
Posts: 12
Hi John,

When I gave it a try it did not work.
But then I found out I made an array go out of bounds with this statement : ${name[3]},
so this went to the 4th element when I passed a 3-element array.

Once I fixed it to : ${name[2]}, then it worked.
See the code below.

So the KEY thing was to declare array in Groovy class : String[] name ;
But I find that strange because this is exactly Java syntax, and in the link to Arrays with Groovy seem to be different than this.

BUT it does work! So thanks a lot!!!



Here it is below:

*********************************************
Java Code (Java has the MAIN):

public static void main(String args[]) {


String[] ArrayStr = { "Google", "Yahoo", "MSN" };

String wordMultiLine;
ProcessG process = new ProcessG();

wordMultiLine = process.getMessage(ArrayStr);

System.out.println(wordMultiLine);


**********************************************
The Groovy code is this::

public class ProcessG {

String[] name


public String getMessage(name) {
assert(name.size() >= 3)
return """
<html>
<body>
Hello, my name is ${name[0]}
and I am returning this from personPlace : ${name[1]}
a multi line GString called : ${name[2]}
</body>
</html>
"""
}

****************************************************************




John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Paul:

It works that way because under the hood, Groovy really is Java. The 'def' keyword basically tells Groovy that you want it to do the dirty work of type specification. You can specify an array, or you can let Groovy handle it, and it will create ArrayLists.

John.
 
GeeCON Prague 2014
 
subject: Java-Groovy : passing arrays