Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine array in groovy?

Tags:

groovy

The following java code exists but I'm trying to convert it to groovy. Should I simply keep it as is w/ the System.arraycopy or does groovy have a nicer way to combine arrays like this?

  byte[] combineArrays(foo, bar, start) {
    def tmp = new byte[foo.length + bar.length]
    System.arraycopy(foo, 0, tmp, 0, start)
    System.arraycopy(bar, 0, tmp, start, bar.length)
    System.arraycopy(foo, start, tmp, bar.length + start, foo.length - start)
    tmp
  }

Thank you

like image 446
JimmyBond07 Avatar asked Feb 04 '11 19:02

JimmyBond07


People also ask

How to merge 2 lists in Groovy?

Combine lists using the plus operator The plus operator will return a new list containing all the elements of the two lists while and the addAll method appends the elements of the second list to the end of the first one. Obviously, the output is the same as the one using addAll method.

What is << in groovy?

In groovy, the bitwise operators can be overridden with the leftShift (<<) and rightShift (>>) methods defined on the class. It's idiomatic groovy to use the leftShift method for append actions on strings, buffers, streams, arrays, etc and thats what you're seeing here.


7 Answers

def a = [1, 2, 3]
def b = [4, 5, 6]

assert a.plus(b) == [1, 2, 3, 4, 5, 6]   
assert a + b     == [1, 2, 3, 4, 5, 6]
like image 144
Thermech Avatar answered Oct 03 '22 20:10

Thermech


If you want to use an array:

def abc = [1,2,3,4] as Integer[] //Array
def abcList = abc as List
def xyz = [5,6,7,8] as Integer[] //Array
def xyzList = xyz as List

def combined = (abcList << xyzList).flatten()

Using Lists:

def abc = [1,2,3,4]
def xyz = [5,6,7,8]
def combined = (abc << xyz).flatten()
like image 21
stan229 Avatar answered Oct 03 '22 20:10

stan229


def a = [1, 2, 3]
def b = [4, 5, 6]
a.addAll(b)
println a

>> [1, 2, 3, 4, 5, 6]

like image 36
altern Avatar answered Oct 03 '22 21:10

altern


The trick is the flatten() method, that combined nested arrays into one:

def a = [1, 2, 3]
def b = [4, 5, 6]
def combined = [a, b].flatten()

assert combined == [1, 2, 3, 4, 5, 6]

println(combined)

To remove null values you can use findAll() like this:

def a = null
def b = [4, 5, 6]
def combined = [a, b].flatten().findAll{it}

assert combined == [4, 5, 6]

println(combined)
like image 28
Brother Avatar answered Oct 03 '22 21:10

Brother


I'd go with

byte[] combineArrays(foo, bar, int start) {
  [*foo[0..<start], *bar, *foo[start..<foo.size()]]
}
like image 43
jpertino Avatar answered Oct 03 '22 22:10

jpertino


It could be done like this:

def newCombine(foo,bar,start) {
   ([].add + foo[0..<start]+bar+foo[start..<foo.size()]).flatten()
}

It works for all kinds of arrays (byte[]) or lists

like image 31
sbglasius Avatar answered Oct 03 '22 22:10

sbglasius


All the solutions above fails if an array is undefined:

def a = [1,2]
def b
assert a+b == [1, 2, null]

which is probably not what you want.

Either test if the array exists before adding:

def a = [1,2,3,4]
def b // null array
def c = [0,4,null,6]
def abc = []
[a,b,c].each{ if (it) abc += it }
assert abc == [1, 2, 3, 4, 0, 4, null, 6] 

,or add all and then filter the output:

(a+b+c).findAll{ it != null }

(assuming here that null isn't a valid value in the original arrays, which implies that the first solution is a lot better, even if it may not look Groovy enough.)

like image 27
sebnukem Avatar answered Oct 03 '22 21:10

sebnukem