为SqList类增加以下的函数:
1、实现线性表的就地逆序: pubic void reverse()
2、实现public void removeRange(int from, int to),删除位置为[from, to]的所有数据。
3、设顺序表中的数据元素递增有序。实现public void insertSortedList(AnyType x),将x插入到顺序表的适当位置上,以保持该表的有序性。
public void reverse() {
if (size() == 0)
return;//判断元素个数是否为0,为0则返回
int left = 0, right = size() - 1;
while (left < right) {//折半转换
T temp = listElem[left];
listElem[left++] = listElem[right];
listElem[right--] = temp;
}
}
public void removeRange(int from, int to) {
checkElementIndex(from);//报错处理
checkElementIndex(to);//报错处理
if (from>to)//判断from是否小于等于to
return;
int i = from;
while ((i++)<=to)
remove(from);//引用remove方法进行删除
}
public void insertSortedList(T x) {
//将T转换成string 对ASCII值进行比较
String[] newlist = new String[size()];
for (int j = 0; j < size(); j++)
newlist[j] = listElem[j]+"";
//当数组的最后一位小于x的ASCII值时放在数组尾部
if (newlist[size-1].compareTo(x+"")<0){
add(size,x);
return;
}
int i= 0;
//寻找i的位置
while (newlist[i].compareTo(x+"")<0)
i++;
//添加
add(i,x);
}
删除from-to的数据不用remove实现
public void removeRange(int from, int to) {
checkElementIndex(from);
checkElementIndex(to);
if (from>to)
return;
for (int i = from,j=to; i <= to;i++,j++){
listElem[i] = listElem[j+1];
listElem[j+1] = null;
size--;
}
}
Sql本来存在的方法
package list;
import java.util.Arrays;
import java.util.Iterator;
import java.util.RandomAccess;
public class SqList<T > implements Ilist<T>, Iterable<T>, RandomAccess, Cloneable {
private T[] listElem;// 存储数据的数组
private int size;// 已经存储的数据的个数
@SuppressWarnings("unchecked")
public SqList(int maxSize) {
listElem = (T[]) new Object[maxSize];
//注意,不能
//listElem = new T[maxSize];
//因为T不是reliable,不能作为数组的元素类型
// size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public int size() {
return size;
}
public void clear() {
// Arrays.fill(listElem, null);//将数组listElem的各元素赋值为null
for (int i = 0; i < size; i++)
listElem[i] = null;// 不写也可以,但为了帮助GC尽快回收
size = 0;
}
public int indexOf(T x) {
for (int i = 0; i < size; i++) {
if (listElem[i].equals(x))
return i;// 必须调用equals,不能listElem[i] == x
}
return -1;
}
// 将位置的合法性检查抽出作为函数
private void checkPositionIndex(int index) {// insert用
if (index < 0 || index > size)
throw new IndexOutOfBoundsException(String.valueOf(index));
}
private void checkElementIndex(int index) {// 其它函数用
if (index < 0 || index > size - 1)
throw new IndexOutOfBoundsException(String.valueOf(index));
}
public T get(int index) {
checkElementIndex(index);
return listElem[index];
}
/*
* private void moveBackward(int pos) { for (int i = size; i > pos; --i)
* listElem[i] = listElem[i - 1]; }
*
* private void moveForward(int pos) { for (int i = pos; i < size; ++i)
* listElem[i] = listElem[i + 1]; }
*/
public void add(int index, T x) {
ensureCapacity(size + 1);
checkPositionIndex(index);
// moveBackward(i);
// if (size - index > 0)//在表尾增加数据
System.arraycopy(listElem, index, listElem, index + 1, size - index);
listElem[index] = x;
++size;
}
public T remove(int index) {
checkElementIndex(index);
T value = listElem[index];
// moveForward(i);
// if(size - index -1 > 0 )//删除表尾的数据
System.arraycopy(listElem, index + 1, listElem, index, size - index - 1);
listElem[--size] = null;
return value;
}
/**
* 以下来自java类库的ArrayList The maximum size of array to allocate. Some VMs reserve
* some header words in an array. Attempts to allocate larger arrays may result
* in OutOfMemoryError: Requested array size exceeds VM limit
*/
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
/**
* Increases the capacity to ensure that it can hold at least the number of
* elements specified by the minimum capacity argument.
*
* @param minCapacity the desired minimum capacity
*/
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = listElem.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
listElem = Arrays.copyOf(listElem, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0) // overflow
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}
private void ensureCapacity(int minCapacity) {
// overflow-conscious code
if (minCapacity - listElem.length > 0)
grow(minCapacity);
}
public String toString() {
StringBuilder str = new StringBuilder(getClass().getName() + ":");
for (int i = 0; i < size; i++)
str.append(listElem[i]).append(" ");
return str + "\n";
}
@SuppressWarnings("unchecked")
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof SqList<?>) {//语法要求,不能写成SqList<T>
SqList<T> rhd = (SqList<T>) obj;
if (this.size != rhd.size)
return false;
// if(Arrays.equals(listElem, rhd.listElem)) return true;
for (int i = 0; i < size; i++) {
if (!listElem[i].equals(rhd.listElem[i]))
return false;
}
return true;
} else
return false;
}
public int hashCode() {
return Integer.valueOf(size).hashCode() * 31 + Arrays.hashCode(listElem);
}
@SuppressWarnings("unchecked")
public Object clone() {
try {
SqList<T> v = (SqList<T>) super.clone();
v.listElem = Arrays.copyOf(listElem, listElem.length);
return v;
} catch (CloneNotSupportedException e) {
// this shouldn't happen, since we are Cloneable
throw new InternalError(e);
}
}
public int capacity() {
return listElem.length;
}
public Iterator<T> iterator() {
return new Itr();
}
private class Itr implements Iterator<T> {
private int curPos;
// public Itr() {
// curPos = 0;
// }
public boolean hasNext() {
return curPos != size;
}
public T next() {
return listElem[curPos++];
}
}
}