这个程序希望实现顺序线性链表,但是用javac编译过后出现如题问题具体是
第45行:return (T)elementData[i];
第86行:T oldValue = (T)elementData[index];
怎样修改源码能不出现警告?
源代码如下:
import java.util.*;
public class SequenceList<T>{
private int DEFAULT_SIZE = 16;
//保存数组的长度
private int capacity;
//定义一个数组用于保存顺序线性列表的元素
private Object[] elementData;
//保存顺序表中元素的当前个数
private int size = 0;
//以默认数组长度创建空顺序线性表
public SequenceList(){
capacity = DEFAULT_SIZE;
elementData = new Object[capacity];
}
//以一个初始化元素创建顺序线性表
public SequenceList(T element){
this();
elementData[0] = element;
size++;
}
/**
*以指定长度的数组来创建顺序线性表
*@param element 指定顺序线性表中第一个元素
*@param initSize 指定顺序线性表底层数组的长度
*/
public SequenceList(T element, int initSize){
capacity = 1;
//把capacity设为大于initSize的最小2的n次方
while(capacity < initSize){
capacity <<= 1;
}
elementData = new Object[capacity];
elementData[0] = element;
size++;
}
//获取顺序线性链表的大小
public int length(){
return size;
}
//获取顺序线性表中索引为i处的元素
public T get(int i){
if(i < 0 || i > size - 1){
throw new IndexOutOfBoundsException("线性表索引越界");
}
return (T)elementData[i];
}
//查找顺序线性列表中指定元素的索引
public int locate(T element){
for(int i = 0; i < size; i++){
if(elementData[i].equals(element))
return i;
}
return -1;
}
//向顺序线性表的指定位置插入一个元素
public void insert(T element, int index){
if(index < 0 || index > size){
throw new IndexOutOfBoundsException("线性表索引越界");
}
ensureCapacity(size + 1);
//将指定索引处之后的所有元素向后移动一格
System.arraycopy(elementData, index, elementData, index+1, size-index);
elementData[index] = element;
size++;
}
//在线性表的开始处添加一个元素
public void add(T element){
insert(element, size);
}
//很麻烦,而且性能很差
private void ensureCapacity(int minCapacity){
//如果数组的原有长度小于目前所需的长度
if(minCapacity > capacity){
//不断的将capacity*2,直到capacity大于minCapacity
while(capacity < minCapacity){
capacity <<= 1;
}
elementData = Arrays.copyOf(elementData, capacity);
}
}
//删除顺序线性表中指定索引处的元素
public T delete(int index){
if(index < 0 || index > size-1){
throw new IndexOutOfBoundsException("线性表索引越界");
}
T oldValue = (T)elementData[index];
int numMoved = size - index - 1;
if(numMoved > 0){
System.arraycopy(elementData, index+1, elementData, index, numMoved);
}
//清空最后一个元素
elementData[--size] = null;
return oldValue;
}
//删除顺序线性表中最后一个元素
public T remove(){
return delete(size-1);
}
//判断顺序线性表是否为空表
public boolean empty(){
return size == 0;
}
//清空线性表
public void clear(){
//将底层数组所有元素赋值为null
Arrays.fill(elementData, null);
size = 0;
}
public String toString(){
if(size == 0){
return "[]";
}else{
StringBuilder sb = new StringBuilder("[");
for(int i = 0; i < size; i++){
sb.append(elementData[i].toString()+",");
}
int len = sb.length();
return sb.delete(len-2, len).append("]").toString();
}
}
public static void main(String[] arg){
SequenceList<String> list = new SequenceList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
list.insert("ddd", 1);
System.out.println(list);
list.delete(2);
System.out.println(list);
System.out.println("ccc在顺序线性表中的位置:"+list.locate("ccc"));
}
}

解决方案 »

  1.   

    奇怪,看了ArrayList的源码,人家写的和你一样,怎么就没事呢?public E remove(int index) {
    RangeCheck(index); modCount++;
    E oldValue = (E) elementData[index]; int numMoved = size - index - 1;
    if (numMoved > 0)
        System.arraycopy(elementData, index+1, elementData, index,
         numMoved);
    elementData[--size] = null; // Let gc do its work return oldValue;
        }
      

  2.   

    ArrayList的 get(index)调用了 elementDate(index)
    // Positional Access Operations    @SuppressWarnings("unchecked")
        E elementData(int index) {
            return (E) elementData[index];
        }lz 使用的是 elementDate[index]
      

  3.   

    我在这两个函数前面加了标签:@SuppressWarnings("unchecked")再拿javac编译就没有警告了,但不知道这个标签什么含义,是忽略警告的意思吗?