Groovy 循环

xiaoxiao2021-02-28  98

Groovy 循环

首先申明下,本文为笔者学习《Groovy 程序设计》的笔记,并加入笔者自己的理解和归纳总结。

1、for-each循环。

(1) 使用冒号形式的for循环,必须指定name的类型。 [java] view plain copy names = ["Michael""James""Kavin""Steven"]  for (String name : names) {      println name  }   返回 Michael James Kavin Steven

(2) 使用in形式的for循环

[java] view plain copy for (name in names) {      println name  }   返回 Michael James Kavin Steven

2、整型方法循环

(1) 范围 [java] view plain copy for (i in 1..3) {      println i  }   返回 1 2 3

(2) upto和downto方法

[java] view plain copy 1.upto(3) {      println it  }  3.downto(1) {      println it  }   返回 1 2 3 3 2 1 (3) times方法 [java] view plain copy 3.times {      println it  }   返回 0 1 2 (4) step方法 [java] view plain copy 1.step(72) {      println it  }   返回 1 3 5
转载请注明原文地址: https://www.6miu.com/read-58938.html

最新回复(0)