def array = [1,2,3,4,5]
def b = int[array.length]
for(int i = 0; i < b.length; i++){
b[i] = Integer.parseInt(array[i])
}
Should I use Integer.parseInt
, Integer.valueOf or other methods?
Should I include a for loop?
def array = [1,2,3,4,5]
def b = int[array.length]
for(int i = 0; i < b.length; i++){
b[i] = Integer.parseInt(array[i])
}
Should I use Integer.parseInt
, Integer.valueOf or other methods?
Should I include a for loop?
Instead of this:
def array = [1,2,3,4,5]
def b = int[array.length]
for(int i = 0; i < b.length; i++){
b[i] = Integer.parseInt(array[i])
}
You could do this:
def array = [1,2,3,4,5]
def b = new int[array.size()]
for(int i = 0; i < b.length; i++){
b[i] = i
}
You could also do this:
def array = [1,2,3,4,5]
def b = array.toArray(Integer[])
In Java 9+, you can stream the matches from a regex:
Pattern.compile("\\d+")
.matcher(s)
.results()
.map(MatchResult::group)
.map(Integer::parseInt)
.collect(Collectors.toList());
Another option: take all numbers from the string as list and then cast them
"1, 2, 3".findAll(/\d+/) as Integer[]