在groovy中使用add或push向数组添加项
在groovy中将项推送/添加到数组时,我遇到以下错误在groovy中使用add或push向数组添加项,groovy,Groovy,在groovy中将项推送/添加到数组时,我遇到以下错误 $groovy main.groovy 捕获:groovy.lang.MissingMethodException:没有方法:[LProgressNotes;.push()的签名适用于参数类型:(ProgressNotes)值:[ProgressNotes@d35dea7] 可能的解决方案:sum()、plus(java.util.Collection)、plus([Ljava.lang.Object;)、plus(java.lang.Ob
$groovy main.groovy
捕获:groovy.lang.MissingMethodException:没有方法:[LProgressNotes;.push()的签名适用于参数类型:(ProgressNotes)值:[ProgressNotes@d35dea7]
可能的解决方案:sum()、plus(java.util.Collection)、plus([Ljava.lang.Object;)、plus(java.lang.Object)、use([Ljava.lang.Object;)、plus(java.lang.Iterable)
groovy.lang.MissingMethodException:没有方法的签名:[LProgressNotes;.push()适用于参数类型:(ProgressNotes)值:[ProgressNotes@d35dea7]
可能的解决方案:sum()、plus(java.util.Collection)、plus([Ljava.lang.Object;)、plus(java.lang.Object)、use([Ljava.lang.Object;)、plus(java.lang.Iterable)
在main$\u buildOutNotes\u closure2.doCall(main.groovy:82)
在main.buildOutNotes(main.groovy:75)
在main$buildOutNotes.callCurrent(未知源)
在main.run(main.groovy:64)
以下是函数:
def buildOutNotes(incomingNotes, systemNotes) {
ProgressNotes[] outNotes = systemNotes;
//iterate incoming chares
incomingNotes.each { incoming ->
//split the note further
def iNote = splitIncoming(incoming);
//check that the incoming note is in the system note
def foundNotes = systemNotes.findAll { it.ProgressNote == iNote.ProgressNote }
if(!foundNotes){
//add the incoming note to the outNote
outNotes.push(iNote);
}
}
return outNotes;
}
下面是展示推送和添加使用的文章
我正在构建示例代码
您可以在这里查看示例
以下是完整的代码:
//package com.javacodegeeks.groovy.date;
//import static java.util.Calendar.*;
//import groovy.json.*;
//import java.util.Properties;
//import java.util.List;
//progress notes object
class ProgressNotes {
def ActionDate
String ActionBy
String Status
String ProgressNote
ProgressNotes(inActionDate, inActionBy, inStatus, inNote){
this.ActionDate = inActionDate
this.ActionBy = inActionBy
this.Status = inStatus
this.ProgressNote = inNote
}
}
//delimiter
String delimiter = "@@";
//out notes
ProgressNotes[] outNotes;
//date patterns
def dateInSystemPattern = "yyyy-MM-dd HH:mm:ss";
def dateIncomingPattern = "MM/dd/yyyy hh:mm ";
/************** SAMPLE DATA START ****************/
//incoming note string
String incomingNote = "2019-12-15T01:29:44 User1: December 13 went to pickup the toilet at the wholesaler " +
"then went to site then remove and replace the toilet then found out that there is a " +
"fruit inside the toilet then clean up the site and silicone around the toilet then " +
"throw the old toilet at dumpster." + delimiter +
"2019-12-13T10:43:05 User2: applied 3 bottles of urinal treatment. let sit for an " +
"hour. augered out urinal main. draining excellent. tried augering toilet. object stuck in " +
"toilet. will not come out. Don will replace." + delimiter +
"2019-12-13T09:18:51 user3: PO 508758 - unclog Washroom " +
"Details: " +
"Unclog toilet bowl and urinal in. Room 116.";
//in system notes
ProgressNotes[] systemNotes = [
["2012-01-26T14:52:50", "User1", "DISPATCHED", "reassign to Space Planning to confirm space availability"],
["2012-02-01T12:23:05", "User2", "DISPATCHED", "spoke to requestor and she has a few relocations and POD requirements."],
["2012-02-01T12:23:45", "User3", "DISPATCHED", "Contacted Customer for clarification spreadsheet is forthcoming for this request."],
["2012-02-03T18:45:00", "User1", "DISPATCHED", "Extending date to allow for clean-up of backlog."]
];
/************** SPLIT incomingNote ****************/
def incomingNotes = [];
if (incomingNote != ""){
incomingNotes = incomingNote.split(delimiter);
}
/************** PICK NOTES ****************/
if (!incomingNotes){
//No incoming notes push the system notes out
outNotes = systemNotes;
}
else{
//check and build the outnotes
outNotes = buildOutNotes(incomingNotes, systemNotes);
}
println("OUTNOTES Length: " + outNotes.length)
println(" ");
/************** HELPER METHODS ****************/
def buildOutNotes(incomingNotes, systemNotes) {
ProgressNotes[] outNotes = systemNotes;
//iterate incoming chares
incomingNotes.each { incoming ->
//split the note further
def iNote = splitIncoming(incoming);
//check that the incoming note is in the system note
def foundNotes = systemNotes.findAll { it.ProgressNote == iNote.ProgressNote }
if(!foundNotes){
//add the incoming note to the outNote
outNotes.push(iNote);
}
}
return outNotes;
}
def splitIncoming(incoming){
//date time characters
int dateTimeChars = 20;
def dateAndTimePart = incoming.substring(0,dateTimeChars).trim();
String remainingNote = incoming.substring(dateTimeChars);
String userPart = "";
String notePart = "";
def remainingNotes = remainingNote.split(":");
if(remainingNotes){
userPart = remainingNotes.getAt(0);
notePart = incoming.substring(dateTimeChars+userPart.length()+1).trim();
}
//build the object
def outNote = new ProgressNotes(dateAndTimePart, userPart, "", notePart);
return outNote;
}
您在代码中使用(ProgressNotes[]
),而不是列表(list
)。所提到的任何方法(add
和push
)都不适用于Java(因此Groovy)数组。数组的大小是固定的,因此一旦初始化,就不能向其中添加任何新元素-只能替换现有元素。如果尝试向数组中添加新元素,则会得到IndexOutOfBoundsException
。只需看以下简单示例:
String[]list=[“foo”,“bar”]
断言列表[0]=“foo”
断言列表[1]=“条”
试一试{
列表[2]=“新建”
}catch(IndexOutOfBoundsException e){
“被抓住了!”
}
列表[1]=“abc”
打印列表
输出:
Caught!
[foo, abc]
如果您想在代码中使用List.add()
或List.push()
(或者像[]一样使用事件groovierleftShift
),则不要使用列表(ProgressNotes[]
),而不是列表(List
)。任何提到的方法(add
和push
)都不适用于Java(因此Groovy)数组。数组的大小是固定的,因此一旦初始化,就不能向其中添加任何新元素-只能替换现有元素。如果尝试向数组中添加新元素,则会得到IndexOutOfBoundsException
。只需看以下简单示例:
String[]list=[“foo”,“bar”]
断言列表[0]=“foo”
断言列表[1]=“条”
试一试{
列表[2]=“新建”
}catch(IndexOutOfBoundsException e){
“被抓住了!”
}
列表[1]=“abc”
打印列表
输出:
Caught!
[foo, abc]
如果您想使用List.add()
或List.push()
(或像[]一样使用事件groovierleftShift
。]感谢您指出我的错误,示例在更改为List
后工作。使用List必须使用.size()
来获得计数。系统注释也已更改List systemNotes=[[“XXX”、“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”]]作为进度注释[];
。样本更新:。感谢您指出我的错误,样本在更改为列表后工作。
。列表中必须使用大小()
获取计数。在系统注释中也更改了列出系统注释=[[“XXX”、“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”]、[“XXX”、“XXX”、“XXX”]]作为进度注释[];
。更新的示例:。
Caught!
[foo, abc]
//out notes
List<ProgressNotes> outNotes;