fbenoit
fbenoit

Reputation: 3260

Groovy remove array element

i have this gradle task to get zip content and put it into another zip. From src zip, i want to take all in directory 'r' and copy it into the target zip directory 'x/y/z'.
The code works, but I wonder if it can be more elegant.

    from( zipTree("a.zip") ) {
        include "r/**"
        includeEmptyDirs = false
        into "x/y/z"
        eachFile { fcd ->
            def segs1 = [fcd.relativePath.segments].flatten().findAll { it2 -> it2 != null };
            segs1.removeAt(3)
            fcd.relativePath = new RelativePath(true, segs1.toArray(new String[0]))
        }
    }

The problem I had is that fcd.relativePath.segments is String[], where i want to remove element with index 3.
Here i convert to list and back to array, brrr.

Ideas?

Frank

Upvotes: 0

Views: 3544

Answers (1)

daggett
daggett

Reputation: 28564

groovy based on java

and in java:

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

so, you could convert it to List, modify size, and then convert back to array

or create a new array with new size and copy required elements into it.

Upvotes: 1

Related Questions