[Groovy] Array and ArrayList

Array:

def x = new String[5]

x[0] = "India"

x[1] = "USA"

x[2] = "Korea"

x[3] = "Japan"

x[4] = "China"



log.info "Size of list is " + x.size()

log.info "The first item in list is : "+x[0]

for(String item:x){

	log.info item

}

 Result :

Tue Jun 16 14:50:08 CST 2015:INFO:Size of list is 5
Tue Jun 16 14:50:08 CST 2015:INFO:The first item in list is : India
Tue Jun 16 14:50:08 CST 2015:INFO:India
Tue Jun 16 14:50:08 CST 2015:INFO:USA
Tue Jun 16 14:50:08 CST 2015:INFO:Korea
Tue Jun 16 14:50:08 CST 2015:INFO:Japan
Tue Jun 16 14:50:08 CST 2015:INFO:China

ArrayList :

ArrayList list = new ArrayList();

list.add("India")

list.add("USA")

list.add("UK")

list.add("China")



log.info "Size of list is "+list.size()

list.add("Korea")



log.info "Size of list is "+list.size()

log.info "The first item in list is : " + list.get(0)



for(String item:list){

	log.info item

}

 

 Result:

Tue Jun 16 14:57:43 CST 2015:INFO:Size of list is 4
Tue Jun 16 14:57:43 CST 2015:INFO:Size of list is 5
Tue Jun 16 14:57:43 CST 2015:INFO:The first item in list is : India
Tue Jun 16 14:57:43 CST 2015:INFO:India
Tue Jun 16 14:57:43 CST 2015:INFO:USA
Tue Jun 16 14:57:43 CST 2015:INFO:UK
Tue Jun 16 14:57:43 CST 2015:INFO:China
Tue Jun 16 14:57:43 CST 2015:INFO:Korea

你可能感兴趣的:(ArrayList)