使用groovy中的add或push将项添加到数组中

时间:2019-12-19 20:29:18

标签: groovy

在将Groovy中的项目推入/添加到数组时,出现以下错误。

$groovy main.groovy Caught: groovy.lang.MissingMethodException: No signature of method: [LProgressNotes;.push() is applicable for argument types: (ProgressNotes) values: [ProgressNotes@d35dea7] Possible solutions: sum(), plus(java.util.Collection), plus([Ljava.lang.Object;), plus(java.lang.Object), use([Ljava.lang.Object;), plus(java.lang.Iterable) groovy.lang.MissingMethodException: No signature of method: [LProgressNotes;.push() is applicable for argument types: (ProgressNotes) values: [ProgressNotes@d35dea7] Possible solutions: sum(), plus(java.util.Collection), plus([Ljava.lang.Object;), plus(java.lang.Object), use([Ljava.lang.Object;), plus(java.lang.Iterable) at main$_buildOutNotes_closure2.doCall(main.groovy:82) at main.buildOutNotes(main.groovy:75) at main$buildOutNotes.callCurrent(Unknown Source) at 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;
} 

以下是显示推送和添加使用的文章

https://mrhaki.blogspot.com/2015/01/groovy-goodness-pop-and-push-items-in.html

def list = ['Groovy', 'is', 'great!']
list.push('rocks!')

http://docs.groovy-lang.org/next/html/documentation/working-with-collections.html

def list = [5, 6, 7, 8]
emptyList.add(5)

我正在https://www.tutorialspoint.com/execute_groovy_online.php上构建示例代码。

您可以在此处查看示例 http://tpcg.io/NGw4szCv

这也是完整的代码:

//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;
}

1 个答案:

答案 0 :(得分:2)

您在代码(ProgressNotes[]中使用an array,而不在列表(List<ProgressNotes>)中使用。 Java(因此也就是Groovy)数组不存在任何上述方法(addpush)。数组是固定大小的,因此一旦初始化,就不能向其添加任何新元素-您只能替换现有元素。如果尝试将新元素添加到数组,则会得到IndexOutOfBoundsException。看看这个简单的例子:

String[] list = ["foo", "bar"]

assert list[0] == "foo"
assert list[1] == "bar"

try {
    list[2] = "new"
} catch (IndexOutOfBoundsException e) {
    println "Caught!"
}

list[1] = "abc"

println list

输出:

Caught!
[foo, abc]

如果您想使用List.add()List.push()(或像leftShift这样的事件groovier [] << "elem"),则需要使用列表而不是数组。如果您知道集合的大小是固定的,则数组是一个不错的选择。

//out notes
List<ProgressNotes> outNotes;