Groovy指向列表的指针

Groovy指向列表的指针,groovy,Groovy,由于我是Groovy的新手,我不知道如何解决这个问题: 我不想从列表中获取新值,该列表取决于输入字符串的值: 简化示例: class NewStringValue { def getValue (inpList) { def list1 = ["L1V1","L1V2","L1V3"]; def list2 = ["L2V1","L2V2","L2V3","L2V4"]; def worklist = Here is my pr

由于我是Groovy的新手,我不知道如何解决这个问题: 我不想从列表中获取新值,该列表取决于输入字符串的值: 简化示例:

class NewStringValue
{

    def getValue (inpList)
    {
        def list1 = ["L1V1","L1V2","L1V3"];
        def list2 = ["L2V1","L2V2","L2V3","L2V4"];
        def worklist =  Here is my problem, how do I get Worklist to point to the correct list according to the value in InpList, see calling ex. below?


     def i = 0;
     def j = worklist.size-1;

     while (i<=j)
      {
      // some code.......
         newValue = worklist[i];
      }
     return newValue;}

您可以使用映射,并根据传入的键查找值:

class NewStringValue {
    def getValue(inpList) {
        def lookup = [
            list1: ["L1V1","L1V2","L1V3"],
            list2: ["L2V1","L2V2","L2V3","L2V4"]
        ]

        def worklist = lookup[inpList]
        def newValue = ''
        worklist.each {
            newValue += it
        }
        newValue
    }
}

new NewStringValue().getValue('list2')
class NewStringValue {
    def getValue(inpList) {
        def lookup = [
            list1: ["L1V1","L1V2","L1V3"],
            list2: ["L2V1","L2V2","L2V3","L2V4"]
        ]

        def worklist = lookup[inpList]
        def newValue = ''
        worklist.each {
            newValue += it
        }
        newValue
    }
}

new NewStringValue().getValue('list2')