How to copy a subset from an array of strings to an array of ints using Groovy?

Posted by Cuga on Stack Overflow See other posts from Stack Overflow or by Cuga
Published on 2010-05-26T16:53:08Z Indexed on 2010/05/26 17:21 UTC
Read the original article Hit count: 208

Filed under:
|
|

I have a String array in a Groovy class (args to a main method):

String[] args

I'd like to convert the 3rd to the last element into a new array of ints. Is there an easier way to do this in Groovy other than:

final int numInts = args.length - 2
final int [] intArray = new int[numInts]
for (int i = 2; i < args.length; i++) {
    intArray[i-2]=Integer.parseInt(args[i])
}

I wanted to do:

final int numInts = args.length - 2
final int [] intArray = new int[numInts]
System.arraycopy(args, 2, intArray, 0, numInts)

But it throws a class cast exception.

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about arrays