以三个为一组的整数列表,并以8个为一组输出它们

时间:2015-01-16 14:32:04

标签: java list groovy output

我正在使用java中的groovy插件进行一些并行练习,但是我已经碰壁了。系统获取包含三个整数但必须在包含整数的对象中输出数据的数据对象。

到目前为止我做了什么:

import org.jcsp.lang.*

class GenerateSetsOfThree implements CSProcess {

    def ChannelOutput outChannel

    void run(){
        def threeList = [
                         [1, 2, 3], 
                         [4, 5, 6], 
                         [7, 8, 9], 
                         [10, 11, 12], 
                         [13, 14, 15], 
                         [16, 17, 18],
                         [19, 20, 21], 
                         [22, 23, 24] ]
        for ( i in 0 ..< threeList.size)outChannel.write(threeList[i])
        //write the terminating List as per exercise definition
        outChannel.write ([-1, -1, -1])

这一切都很好。 然后:

import org.jcsp.lang.*

class ListToStream implements CSProcess{

    def ChannelInput inChannel
    def ChannelOutput outChannel

    void run (){
        def inList = inChannel.read()
        while (inList[0] != -1) {
            // hint: output list elements as single integers
        outChannel.write (inList[0, 1, 2, 3, 4, 5, 6, 7])

        }
        outChannel.write(-1)
    }
}

这是问题出现的地方,我不知道如何取整数并将它们分组为8个

最后:

import org.jcsp.lang.*

class CreateSetsOfEight implements CSProcess{

    def ChannelInput inChannel

    void run(){
        def outList = []
        def v = inChannel.read()
        while (v != -1){
            for ( i in 0 .. 7 ) {
                // put v into outList and read next input
                outList = [(v)]
                v = inChannel.read()    
            }
            println " Eight Object is ${outList}"
        }
        println "Finished"
    }
}

这一切似乎都很好。

任何建议都将不胜感激。 谢谢。

1 个答案:

答案 0 :(得分:0)

你不应该在ListToStream中将它们转换为octuples。首先,他们在那里转换为单一价值。转换为八元组是在最后一步CreateSetsOfEight完成的。传递单个值,您可以自己发出每个值。

e.g。

while (inList[0] != -1) {
    // hint: output list elements as single integers
    inList.each{ outChannel.write it }
    inList = inChannel.read()
}

然而,所有的答案都在线:https://github.com/GPars/GPars/tree/master/docs/JonKerridgeBook