Groovy中的等效代码

时间:2015-02-14 03:38:11

标签: java groovy

在java中有以下代码的groovy中的等效代码:

import java.util.Arrays;
import java.util.List;
import java.util.Random;

class Text {

public static void main(String [] args) {

String [] array = {"hello", "world","Pasci","Jenny"};
List <String> list = Arrays.asList(array);
 {
     Random rand = new Random();
     System.out.println("String from list: " + list.get(rand.nextInt(list.size())));
}
}
}

谢谢

1 个答案:

答案 0 :(得分:1)

Groovy的语法更轻,而不像Java那样冗长:

def list = ["hello", "world","Pasci","Jenny"]
Random rand = new Random()
println "String from list: " + list.get(rand.nextInt(list.size()))

旁注:Groovy几乎是Java的超级集合,这意味着,您可以在groovy文件中编写几乎任何 Java代码,并且它可以编译然后跑。