Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 如何制作一个包含数组的映射,并访问该数组的每个索引?_List_Groovy - Fatal编程技术网

List 如何制作一个包含数组的映射,并访问该数组的每个索引?

List 如何制作一个包含数组的映射,并访问该数组的每个索引?,list,groovy,List,Groovy,基本上,我在底部有一些这样的代码。这会创建很多列表,但是否可以将所有这些列表放在地图中?此外,我访问for循环中的每个索引,因此是否可以在包含列表的映射中访问这些索引?我只是想实现更短的代码,可能更高效 //Create our lists def htmlList = [] def pixProductList = [] def pixLanguageList = [] def pixOffercodeList = [] def pixVIDList = [] def pixStartDate

基本上,我在底部有一些这样的代码。这会创建很多列表,但是否可以将所有这些列表放在地图中?此外,我访问for循环中的每个索引,因此是否可以在包含列表的映射中访问这些索引?我只是想实现更短的代码,可能更高效

//Create our lists
def htmlList = []
def pixProductList = []
def pixLanguageList = []
def pixOffercodeList = []
def pixVIDList = []
def pixStartDateList = []
def pixEndDateList = []
def pixContactList = []
def pixPublisherList = []
def newPixelList = []

//Parse the file
String file = new File('grails-app/controllers/pixel/editor/tool/trackingPixels.xml').text
newPixelList = StringUtils.substringsBetween(file, "<pixelNew", "</pixelNew>")

//Access each element in newPixelList 
for(int i =0; i < newPixelList.size(); i++){
    String newPixel = newPixelList[i]
    htmlList[i] = StringUtils.substringBetween(newPixel, "<html>", "</html>")
    pixProductList[i] = StringUtils.substringBetween(newPixel, "<product>", "</product>")
    pixLanguageList[i] = StringUtils.substringBetween(newPixel, "<lang>", "</lang>")
    pixOffercodeList[i] = StringUtils.substringBetween(newPixel, "<offercode>", "</offercode>")
    pixVIDList[i] = StringUtils.substringBetween(newPixel, "<vid>", "</vid>")
    pixStartDateList[i] = StringUtils.substringBetween(newPixel, "<startDate>", "</startDate>")
    pixEndDateList[i] = StringUtils.substringBetween(newPixel, "<endDate>", "</endDate>")
    pixContactList[i] = StringUtils.substringBetween(newPixel, "<contact>", "</contact>")
    pixPublisherList[i] = StringUtils.substringBetween(newPixel, "<publisher>", "</publisher>")
    }
//创建我们的列表
def htmlist=[]
def pixProductList=[]
def pixLanguageList=[]
def pixOffercodeList=[]
def pixVIDList=[]
def pixStartDateList=[]
def pixEndDateList=[]
def pixContactList=[]
def pixPublisherList=[]
def newPixelList=[]
//解析文件
字符串文件=新文件('grails-app/controllers/pixel/editor/tool/trackingPixels.xml')。文本

newPixelList=StringUtils.substringsBetween(文件“首先,您可以实际使用XML slurper并使用它来代替此字符串操作

def htmlList = []
def pixProductList = []
def pixLanguageList = []
def pixOffercodeList = []
def pixVIDList = []
def pixStartDateList = []
def pixEndDateList = []
def pixContactList = []
def pixPublisherList = []
def newPixelList = []

File file = new File('grails-app/controllers/pixel/editor/tool/trackingPixels.xml')
def xmlFile = new XmlSlurper().parse(file)
def records = xmlFile.pixelNew //Assuming pixelNew is the top level node

records.each {
    htmlList.add(it.html.text())
    pixProductList.add(it.product.text())
    ...
}
然后您可以创建一个类来存储这些数据,并在构造函数中填充数据

class WhateverYouWant {
  String html
  String product
  ...

  public WhateverYouWant(NodeChild record) {
      this.html = record.html.text()
      this.product = record.product.text()
      ...
  }
}
然后你可以简单地做:

List<WhateverYouWant> items = []

xmlFile = new XmlSlurper().parse(file)
def records = xmlFile.pixelNew //Assuming pixelNew is the top level node

records.each {
    items.add(new WhateverYouWant(it))
}
列出项目=[]
xmlFile=new XmlSlurper().parse(文件)
def records=xmlFile.pixelNew//假设pixelNew是顶级节点
记录,每个{
项目。添加(新的您想要的内容(it))
}

Man你很快。谢谢。在我开始打字之前,我做了一次刷新,看看是否已经提交了答案。你回答的正是我所想的。:-)谢谢!我在目前正在进行的项目中做了几乎完全相同的事情,这对我很有帮助。所以主要是复制/粘贴。这方面的好处是,您可以重载构造函数以支持具有相同数据的不同文件类型。当您使用XML Slurper时,您不需要使用parseText()而不是parse()?因为“文件”“字符串不是文件吗?我错了。我编辑以获得答案的清晰图像。请参阅updatenow@EliDavilaWell我现在的问题是,如何访问这些项目?我尝试了类似println items[0]的东西,它告诉我一些关于未保存元素的信息。