Groovy - Formas de acessar índices de uma lista e obter ou "null" ou uma exceção

Posted Over 7 years ago. Visible to the public.
List listaVazia = []

// ------ QUALQUER ÍNDICE

def x = listaVazia[2]
println x // null

def y = listaVazia.getAt(2)
println y // null

//listaVazia.get(2) // java.lang.IndexOutOfBoundsException: Index: 2, Size: 0


// ------ APENAS ÍNDICE 0 (ZERO)

def z = listaVazia.find()
println z // null , mas é exclusivo para o índice 0 igual o first() pois não aceita parâmetros para acessar outros índices

//listaVazia.first() // java.util.NoSuchElementException: Cannot access first() element from an empty List
Bruno Vieira
Last edit
Over 7 years ago
Bruno Vieira
Posted by Bruno Vieira to ZeroGlosa (2016-09-20 21:32)