wldomiciano 23 de jun. de 2021 2 likes
De acordo com a documentação: https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Arrays.html#asList(T...)
O método asList() retorna uma lista que implementa os métodos de Collection exceto os métodos que alteram o tamanho da lista.
The returned list implements the optional Collection methods, except those that would change the size of the returned list. Those methods leave the list unchanged and throw UnsupportedOperationException .
Curiosidade:
Se vc olhar o código fonte do método asList(), verá que ele retorna um ArrayList.
* {@link Collections#unmodifiableList Collections.unmodifiableList}
* or <a href="List.html#unmodifiable">Unmodifiable Lists</a>.
*
* @param <T> the class of the objects in the array
* @param a the array by which the list will be backed
* @return a list view of the specified array
* @throws NullPointerException if the specified array is {@code null}
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
@java.io.Serial
Mas não se deixe enganar. Este ArrayList não é este que usamos sempre:
* @param <E> the type of elements in this list
*
* @author Josh Bloch
* @author Neal Gafter
* @see Collection
* @see List
* @see LinkedList
* @see Vector
* @since 1.2
*/
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
@java.io.Serial
private static final long serialVersionUID = 8683452581122892189L;
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
Mas sim, este aqui que é declarado dentro da própria classe Arrays:
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
/**
* @serial include
*/
private static class ArrayList<E> extends AbstractList<E>
implements RandomAccess, java.io.Serializable
{
@java.io.Serial
private static final long serialVersionUID = -2764017481108945198L;
@SuppressWarnings("serial") // Conditionally serializable
private final E[] a;
ArrayList(E[] array) {
a = Objects.requireNonNull(array);
}
DimiMartins 23 de jun. de 2021 1 like