Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
102 Chapter 5 Global Context Listing 5.42 Testing if the Global Context Contains a Particular Variable if(app.string[]) { //do something } Like zcontains, this can be used as a guard code that needs to have particular variables available. In Groovy, there are no shortcuts for other Java methods, so we would just use the Java version in Groovy. Lists Just like Java, in Groovy, lists are indexed lists of objects. To put a single list variable into the global context in Java, we use the zput method. zput Method To put a list into the global context, we use the zput method: boolean zput(String location, List list); In. | 102 Chapter 5 Global Context Listing 5.42 Testing if the Global Context Contains a Particular Variable if app.string do something Like zcontains this can be used as a guard code that needs to have particular variables available. In Groovy there are no shortcuts for other Java methods so we would just use the Java version in Groovy. Lists Just like Java in Groovy lists are indexed lists of objects. To put a single list variable into the global context in Java we use the zput method. zput Method To put a list into the global context we use the zput method boolean zput String location List list In Groovy we can simply make a direct assignment tmp.list 42 string object With this code we ve created a list and directly created or replaced the list in the tmp zone. We can replace single elements of our stored list by using array style indexes to specify where to put the value tmp.list 1 new string This replaces string object with new string in our stored list. zpost Method To append to a list in the global context we use zpost in Groovy just like we do in Java. Please refer to the previous section on Java. zget Method In Java to retrieve lists placed in the global context we use the zget method Object zget String location Object defaultvalue In Groovy we can access lists in the global context directly see Listing 5.43 . Listing 5.43 Retrieving Lists from the Global Context with Groovy var slist app.stringList var ilist app.integerList Download from www.wowebook.com Accessing the Global Context 103 Here we are retrieving our string and integer lists and assigning them to a Groovy variable directly. We can also retrieve individual elements from our list by again using the array style indexing notation see Listing 5.44 . Listing 5.44 Retrieving an Element from a List var s app.stringList 0 var i app.integerList 0 This code retrieves the first element of each list. zdelete Method To remove a list from the global context we use zdelete in Groovy just like we do in Java. Please