This should work:
It will print you "red".
But if you want to print all the values of your list, you can use your foreach
like that:
This code will iterate over your list colors
and set each element of this list in a variable called element
. So, element
has the same type as the one who parameterized your list (here, String
). So you can not call get(1)
on String
since it does not exist.
So you call directly
which will call toString()
method of your current element.