Home > Java > javaTutorial > How to use aslist in Java

How to use aslist in Java

WBOY
Release: 2023-04-18 21:55:05
forward
1052 people have browsed it

Function

1. Returns a fixed-size list supported by the specified array.

2. The ArrayList returned by this method is not our commonly used collection class java.util.ArrayList. ArrayList is the internal class java.util.Arrays.ArrayList of Arrays.

Example

private static class ArrayList<E> extends AbstractList<E>
        implements RandomAccess, java.io.Serializable
    {
        private static final long serialVersionUID = -2764017481108945198L;
        private final E[] a;
 
        ArrayList(E[] array) {
            a = Objects.requireNonNull(array);
        }
 
        @Override
        public int size() {
            return a.length;
        }
 
        @Override
        public Object[] toArray() {
            return a.clone();
        }
 
        @Override
        @SuppressWarnings("unchecked")
        public <T> T[] toArray(T[] a) {
            int size = size();
            if (a.length < size)
                return Arrays.copyOf(this.a, size,
                                     (Class<? extends T[]>) a.getClass());
            System.arraycopy(this.a, 0, a, 0, size);
            if (a.length > size)
                a[size] = null;
            return a;
        }
 
        @Override
        public E get(int index) {
            return a[index];
        }
 
        @Override
        public E set(int index, E element) {
            E oldValue = a[index];
            a[index] = element;
            return oldValue;
        }
 
        @Override
        public int indexOf(Object o) {
            E[] a = this.a;
            if (o == null) {
                for (int i = 0; i < a.length; i++)
                    if (a[i] == null)
                        return i;
            } else {
                for (int i = 0; i < a.length; i++)
                    if (o.equals(a[i]))
                        return i;
            }
            return -1;
        }
 
        @Override
        public boolean contains(Object o) {
            return indexOf(o) != -1;
        }
 
        @Override
        public Spliterator<E> spliterator() {
            return Spliterators.spliterator(a, Spliterator.ORDERED);
        }
 
        @Override
        public void forEach(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            for (E e : a) {
                action.accept(e);
            }
        }
 
        @Override
        public void replaceAll(UnaryOperator<E> operator) {
            Objects.requireNonNull(operator);
            E[] a = this.a;
            for (int i = 0; i < a.length; i++) {
                a[i] = operator.apply(a[i]);
            }
        }
 
        @Override
        public void sort(Comparator<? super E> c) {
            Arrays.sort(a, c);
        }
    }
Copy after login

The above is the detailed content of How to use aslist in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template