Java : AbstractSequentialList 示例

Java 中的 AbstractSequentialList (Java SE 23 & JDK 23) 及其示例。
您将找到大多数 AbstractSequentialList<E> 方法的代码示例。

注解 :

  • 本文可能使用了翻译软件以方便阅读。 另请查看英文原文

简介

此类提供了 List 接口的骨架实现,以尽量减少实现由“顺序访问”数据存储(例如链接列表)支持的此接口所需的工作量。 (机器翻译)

Class diagram

class MyList extends AbstractSequentialList<String> {
    private final String[] values;

    MyList(String[] values) {
        this.values = Objects.requireNonNull(values);
    }

    @Override
    public ListIterator<String> listIterator(int index) {
        return new ListIteratorImpl(index);
    }

    @Override
    public int size() {
        return values.length;
    }

    private class ListIteratorImpl implements ListIterator<String> {
        private int cursor;

        private ListIteratorImpl(int index) {
            if (index < 0 || index > size()) {
                throw new IndexOutOfBoundsException();
            }

            this.cursor = index;
        }

        @Override
        public boolean hasNext() {
            return size() != cursor;
        }

        @Override
        public String next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }

            final var value = values[cursor];
            cursor++;
            return value;
        }

        @Override
        public boolean hasPrevious() {
            return 0 != cursor;
        }

        @Override
        public String previous() {
            if (!hasPrevious()) {
                throw new NoSuchElementException();
            }

            cursor--;
            return values[cursor];
        }

        @Override
        public int nextIndex() {
            return cursor;
        }

        @Override
        public int previousIndex() {
            return cursor - 1;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void set(String s) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void add(String s) {
            throw new UnsupportedOperationException();
        }
    }
}
final var list = new MyList(new String[]{"aaa", "bbb", "ccc"});
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.size()); // 3
System.out.println(list.isEmpty()); // false

System.out.println("-- values --");
for (final var value : list) {
    System.out.println(value);
}

// Result
// ↓
//-- values --
//aaa
//bbb
//ccc

Fields declared in AbstractList

modCount

请参阅下面的链接。

Constructors

AbstractSequentialList ()

唯一建造者。 (机器翻译)

class MyList extends AbstractSequentialList<String> {
    private final String[] values;

    MyList(String[] values) {
        this.values = Objects.requireNonNull(values);
    }

    @Override
    public ListIterator<String> listIterator(int index) {
        return new ListIteratorImpl(index);
    }

    @Override
    public int size() {
        return values.length;
    }

    private class ListIteratorImpl implements ListIterator<String> {
        private int cursor;

        private ListIteratorImpl(int index) {
            if (index < 0 || index > size()) {
                throw new IndexOutOfBoundsException();
            }

            this.cursor = index;
        }

        @Override
        public boolean hasNext() {
            return size() != cursor;
        }

        @Override
        public String next() {
            if (!hasNext()) {
                throw new NoSuchElementException();
            }

            final var value = values[cursor];
            cursor++;
            return value;
        }

        @Override
        public boolean hasPrevious() {
            return 0 != cursor;
        }

        @Override
        public String previous() {
            if (!hasPrevious()) {
                throw new NoSuchElementException();
            }

            cursor--;
            return values[cursor];
        }

        @Override
        public int nextIndex() {
            return cursor;
        }

        @Override
        public int previousIndex() {
            return cursor - 1;
        }

        @Override
        public void remove() {
            throw new UnsupportedOperationException();
        }

        @Override
        public void set(String s) {
            throw new UnsupportedOperationException();
        }

        @Override
        public void add(String s) {
            throw new UnsupportedOperationException();
        }
    }
}
final var list = new MyList(new String[]{"aaa", "bbb", "ccc"});
System.out.println(list); // [aaa, bbb, ccc]
System.out.println(list.size()); // 3
System.out.println(list.isEmpty()); // false

System.out.println("-- values --");
for (final var value : list) {
    System.out.println(value);
}

// Result
// ↓
//-- values --
//aaa
//bbb
//ccc

Methods

void add (int index, E element)

将指定元素插入此列表的指定位置(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(0, "A");
    System.out.println(list); // [A, a, b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(1, "B");
    System.out.println(list); // [a, B, b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(2, "C");
    System.out.println(list); // [a, b, C, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    list.add(3, "D");
    System.out.println(list); // [a, b, c, D]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.add(4, "D"); // IndexOutOfBoundsException
}

boolean addAll (int index, Collection<? extends E> c)

将指定集合中的所有元素插入此列表中的指定位置(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(0, List.of("A1", "A2"))); // true
    System.out.println(list); // [A1, A2, a, b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(1, List.of("B1", "B2", "B3"))); // true
    System.out.println(list); // [a, B1, B2, B3, b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(2, List.of("C1", "C2", "C3", "C4"))); // true
    System.out.println(list); // [a, b, C1, C2, C3, C4, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.addAll(3, List.of())); // false
    System.out.println(list); // [a, b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.addAll(4, List.of("E")); // IndexOutOfBoundsException
}
final var src = List.of("a", "b", "c");
System.out.println(src);// [a, b, c]

final AbstractSequentialList<String> list = new LinkedList<>(src);

System.out.println(list.addAll(3, Set.of("D1", "D2", "D3"))); // true
System.out.println(list); // [a, b, c, D3, D2, D1]

E get (int index)

返回此列表中指定位置的元素。 (机器翻译)

final AbstractSequentialList<String> list = new LinkedList<>();
Collections.addAll(list, "a", "b", "c");
System.out.println(list); // [a, b, c]

//list.get(-1); // IndexOutOfBoundsException
System.out.println(list.get(0)); // a
System.out.println(list.get(1)); // b
System.out.println(list.get(2)); // c
//list.get(3); // IndexOutOfBoundsException
final AbstractSequentialList<Integer> list = new LinkedList<>();
Collections.addAll(list, 10, 20, 30, 40);
System.out.println(list); // [10, 20, 30, 40]

System.out.println(list.get(0)); // 10
System.out.println(list.get(1)); // 20
System.out.println(list.get(2)); // 30
System.out.println(list.get(3)); // 40

Iterator<E> iterator ()

返回此列表中元素的迭代器(按正确的顺序)。 (机器翻译)

final AbstractSequentialList<String> list = new LinkedList<>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

final var iterator = list.iterator();
iterator.forEachRemaining(System.out::println);

// Result
// ↓
//aaa
//bbb
//ccc

abstract ListIterator<E> listIterator (int index)

返回此列表中元素的列表迭代器(按正确的顺序)。 (机器翻译)

final AbstractSequentialList<String> list = new LinkedList<>();
Collections.addAll(list, "aaa", "bbb", "ccc");
System.out.println(list); // [aaa, bbb, ccc]

{
    final var ret = list.listIterator(0);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //aaa
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(1);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //bbb
    //ccc
}
{
    final var ret = list.listIterator(2);
    ret.forEachRemaining(System.out::println);

    // Result
    // ↓
    //ccc
}

E remove (int index)

删除此列表中指定位置的元素(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(0)); // a
    System.out.println(list); // [b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(1)); // b
    System.out.println(list); // [a, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.remove(2)); // c
    System.out.println(list); // [a, b]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.remove(3); // IndexOutOfBoundsException
}

E set (int index, E element)

用指定元素替换此列表中指定位置的元素(可选操作)。 (机器翻译)

final var src = List.of("a", "b", "c");
System.out.println(src); // [a, b, c]

{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(0, "A")); // a
    System.out.println(list); // [A, b, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(1, "B")); // b
    System.out.println(list); // [a, B, c]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    System.out.println(list.set(2, "C")); // c
    System.out.println(list); // [a, b, C]
}
{
    final AbstractSequentialList<String> list = new LinkedList<>(src);
    System.out.println(list); // [a, b, c]

    //list.set(3, "D"); // IndexOutOfBoundsException
}

Methods declared in AbstractList

add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, removeRange, subList

请参阅下面的链接。

Methods declared in AbstractCollection

addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString

请参阅下面的链接。

Methods declared in Collection

parallelStream, removeIf, stream, toArray

请参阅下面的链接。

Methods declared in Iterable

forEach

请参阅下面的链接。

Methods declared in List

addAll, addFirst, addLast, contains, containsAll, getFirst, getLast, isEmpty, remove, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, size, sort, spliterator, toArray, toArray

请参阅下面的链接。


相关文章

To top of page