数组 – 按字母顺序排列字符串数组
发布时间:2020-12-14 16:31:02 所属栏目:大数据 来源:网络整理
导读:所以我一直在学习使用Groovy中的数组.我想知道如何按字母顺序排列字符串数组.我的代码当前从用户获取字符串输入并按顺序打印出来: System.in.withReader { def country = [] print 'Enter your ten favorite countries:' for (i in 0..9) country it.readLi
所以我一直在学习使用Groovy中的数组.我想知道如何按字母顺序排列字符串数组.我的代码当前从用户获取字符串输入并按顺序打印出来:
System.in.withReader { def country = [] print 'Enter your ten favorite countries:' for (i in 0..9) country << it.readLine() print 'Your ten favorite countries in order/n' println country //prints the array out in the order in which it was entered print 'Your ten favorite countries in reverse' country.reverseEach { println it } //reverses the order of the array 我如何按字母顺序打印出来? 解决方法
sort()是你的朋友.
country.sort()将按照字母顺序对突变国家进行排序. def country = ['Ireland','Iceland','Hungary','Thailand'] assert country.sort() == ['Hungary','Ireland','Thailand'] assert country == ['Hungary','Thailand'] country = ['Ireland','Thailand'] assert country.sort(false) == ['Hungary','Thailand'] assert country == ['Ireland','Thailand'] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |