compare array and arraylist of groovy ; arrayList is alway useful
Operation | Array | ArrayList |
---|---|---|
Initialization | def arr = [1, 2, 3] | def list = [1, 2, 3] as ArrayList |
Create Empty | def arr = [] as int[] | def list = new ArrayList() |
Add Element | Not directly supported | list.add(element) or list << element |
Add Multiple Elements | Not directly supported | list.addAll([element1, element2]) |
Access Element by Index | arr[index] | list[index] |
Modify Element by Index | arr[index] = value | list[index] = value |
Remove Element by Index | Not directly supported | list.remove(index) |
Remove Element by Value | Not directly supported | list.remove(value) |
Check if Empty | arr.isEmpty() | list.isEmpty() |
Get Size | arr.size() | list.size() |
Iterate (for-each loop) | for (element in arr) | for (element in list) |
Iterate (for loop) | for (i = 0; i < arr.size(); i++) | for (i = 0; i < list.size(); i++) |
Contains Element | element in arr | element in list |
Sort (In-place) | arr.sort() | list.sort() |
Sort (Create Sorted Copy) | def sortedArr = arr.sort(false) | def sortedList = list.sort(false) |
Join Elements into String | arr.join(", ") | list.join(", ") |