在自定义ArrayList中实现subList()方法,可以按照以下步骤进行:
下面是一个示例代码:
import java.util.AbstractList;
import java.util.List;
public class CustomArrayList<E> extends AbstractList<E> {
private Object[] elements;
private int size;
public CustomArrayList() {
elements = new Object[10];
size = 0;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return (E) elements[index];
}
@Override
public int size() {
return size;
}
@Override
public E set(int index, E element) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
E oldValue = (E) elements[index];
elements[index] = element;
return oldValue;
}
@Override
public void add(int index, E element) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
if (size == elements.length) {
resize();
}
System.arraycopy(elements, index, elements, index + 1, size - index);
elements[index] = element;
size++;
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
E oldValue = (E) elements[index];
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elements, index + 1, elements, index, numMoved);
}
elements[--size] = null;
return oldValue;
}
private void resize() {
Object[] newElements = new Object[elements.length * 2];
System.arraycopy(elements, 0, newElements, 0, size);
elements = newElements;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) {
throw new IndexOutOfBoundsException();
}
return new SubList(fromIndex, toIndex);
}
private class SubList extends AbstractList<E> {
private final int offset;
private final int size;
SubList(int fromIndex, int toIndex) {
offset = fromIndex;
size = toIndex - fromIndex;
}
@Override
public E get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
return (E) elements[offset + index];
}
@Override
public int size() {
return size;
}
@Override
public E set(int index, E element) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
E oldValue = (E) elements[offset + index];
elements[offset + index] = element;
return oldValue;
}
@Override
public void add(int index, E element) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException();
}
if (CustomArrayList.this.size == elements.length) {
resize();
}
System.arraycopy(elements, offset + index, elements, offset + index + 1, size - index);
elements[offset + index] = element;
CustomArrayList.this.size++;
size++;
}
@Override
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
E oldValue = (E) elements[offset + index];
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elements, offset + index + 1, elements, offset + index, numMoved);
}
elements[offset + --CustomArrayList.this.size] = null;
size--;
return oldValue;
}
private void resize() {
Object[] newElements = new Object[elements.length * 2];
System.arraycopy(elements, 0, newElements, 0, CustomArrayList.this.size);
elements = newElements;
}
@Override
public List<E> subList(int fromIndex, int toIndex) {
if (fromIndex < 0 || toIndex > size || fromIndex > toIndex) {
throw new IndexOutOfBoundsException();
}
return new SubList(offset + fromIndex, offset + toIndex);
}
}
}
这样,你就可以在自定义ArrayList中使用subList()方法来获取子列表了。
企业创新在线学堂
企业创新在线学堂
云+社区技术沙龙[第17期]
企业创新在线学堂
云原生正发声
Elastic 中国开发者大会
云原生正发声
GAME-TECH
领取专属 10元无门槛券
手把手带您无忧上云