The code copy is as follows:
import java.util.Arrays;
/**
* Implementation of sequential linear tables
*/
public class LineList<E>{
private int size;//Length
private Object[] array;//The underlying array
private final int default_length=16;//Default length
/**
* Non-parameter construction method
*/
public LineList(){
size = 0;
//Construct an array using the default length
array = new Object[default_length];
}
/**
* Specify the length for construction
* @param length Specify the initial length
*/
public LineList(int length){
if(length<0){
throw new IllegalArgumentException("Initial length is illegal:"+length);
}
//Construct an array with a specified length
array = new Object[length];
}
/**
* Specify the initialization element and length for construction
* @param element Initialize element
* @param length Initialization length
*/
public LineList(E element,int length){
if(length<1){
throw new IllegalArgumentException("Initial length is illegal:"+length);
}
//Construct an array with a specified length
array = new Object[length];
//Initialize the first element
array[0] = element;
size++;
}
/**
* Specify initialization elements for construction
* @param element Initialize element
*/
public LineList(E element){
//Initialize the array with default length
array = new Object[default_length];
//Initialize the first element
array[0] = element;
}
/**
* Get the number of elements
*/
public int size() {
return size;
}
/**
* Determine whether it is empty
*/
public boolean isEmpty() {
return size==0;
}
/**
* Determine whether this element is included
*/
public boolean contains(E e) {
if(indexOf(e) == -1){
return false;
}
return true;
}
/**
* Format as an array
*/
public Object[] toArray() {
return Arrays.copyOf(array, size);
}
/**
* Add an element to the tail of the linear table
* @param e
* @return
*/
public void add(E e) {
extendCapacity(size+1);
array[size]=e;
size++;
}
/**
* Expand capacity
* @param length The required length
*/
private void extendCapacity(int length){
//The current array length and required length should be the maximum
int minCapacity = Math.max(array.length, length);
//Judge whether capacity expansion is required
if(minCapacity - array.length>0){
//Array length is increased by half
int newLength = array.length + array.length/2;
//If the new length is smaller than the requirement, use the required length as the array length
if(newLength < minCapacity){
newLength=minCapacity;
}
//The array length cannot exceed Integer.Max_Value
if(newLength > Integer.MAX_VALUE - 8){
newLength = Integer.MAX_VALUE;
}
//Array expansion
array = Arrays.copyOf(array, newLength);
}
}
/**
* Remove all this element from the linear table
* @param e Elements that need to be removed
* @return
*/
public void removeAll(E e) {
if(e==null){
for(int i=0;i<size;i++){
if(array[i]==null){
fastRemove(i);
}
}
}else{
for(int i=0;i<size;i++){
if(e.equals(array[i])){
fastRemove(i);
}
}
}
}
/**
* Delete the element at the index, and move the following elements forward in turn
* @param index The index that needs to be deleted
*/
private void fastRemove(int index){
if(size-index-1>0){
//Arrays start from index+1 and all move forward
System.arraycopy(array, index+1, array, index, size-1);
}
//Please empty the last element
array[--size]=null;
}
/**
* Clear the linear table
*/
public void clear() {
//Fill all arrays into null
Arrays.fill(array, null);
//Change the number of elements to 0
size=0;
}
/**
* Get the element at the index
* @param index
* @return Element at index
*/
@SuppressWarnings("unchecked")
public E get(int index) {
checkIndex(index);
return (E)array[index];
}
/**
* Verify whether the index is out of bounds
* @param index
*/
private void checkIndex(int index){
if(index>=size || index<0){
throw new IndexOutOfBoundsException("IndexOutBoundsException");
}
}
/**
* Modify the element at the index into a new element
* @param index index position
* @param element
* @return Element at the original index
*/
@SuppressWarnings("unchecked")
public E set(int index, E element) {
checkIndex(index);
E e = (E)array[index];
array[index]=element;
return e;
}
/**
* Insert the specified element at the specified index
* @param index index position
* @param element
*/
public void add(int index, E element) {
//Verify the index
checkIndex(index);
//Is it necessary to expand the capacity
extendCapacity(size+1);
//Copy the array
System.arraycopy(array, index, array, index+1, size-index);
array[index]=element;
}
/**
* Remove elements at the index
* @param index index
* @return Deleted elements
*/
@SuppressWarnings("unchecked")
public E remove(int index) {
checkIndex(index);
//Get the index position element
E e = (E)array[index];
fastRemove(index);
return e;
}
/**
* Get the index of the location where the element first appears
* @param e Element to look for
* @return If it is -1, it means that the linear table does not have this element
*/
public int indexOf(E e) {
if(e==null){
for(int i=0;i<size;i++){
if(e==array[i]){
return i;
}
}
}
for(int i=0;i<size;i++){
if(e.equals(array[i])){
return i;
}
}
return -1;
}
/**
* Get the index of the last occurrence of the element
* @param e Element to look for
* @return If it is -1, it means that the linear table does not have this element
*/
public int lastIndexOf(E e) {
//Judge whether the element is null
if(e==null){
for(int i=size-1;i>=0;i--){
if(e==array[i]){
return i;
}
}
}
for(int i=size-1;i>=0;i--){
//If null, a NullPoint exception will be run here
//So you need to add the verification whether it is Null
if(e.equals(array[i])){
return i;
}
}
return -1;
}
/**
* Intercept the linear table
* @param fromIndex Start indexing
* @param toIndex End Index
* @return Intercepted linear table
*/
@SuppressWarnings("unchecked")
public LineList<E> subList(int fromIndex, int toIndex) {
//Judge whether the start index is out of bounds
if(fromIndex<0 || fromIndex >=size){
throw new IndexOutOfBoundsException("Start index cross-border:"+fromIndex);
}
//Judge whether the end index is out of bounds
if(toIndex >=size || fromIndex <0){
throw new IndexOutOfBoundsException("End Index Outbound:"+toIndex);
}
//Judge whether the start index and end index are correct
if(fromIndex > toIndex){
throw new IllegalArgumentException("The parameter is incorrect, the start index should be greater than or equal to the end index");
}
LineList<E> list = new LineList<E>();
for(int i=fromIndex,j=toIndex;i<=j;i++){
list.add((E)array[i]);
}
return list;
}
}