Java 使用泛型并尝试消除不安全操作警告
这是我的代码,但我一直得到“注意:LinkedListAdd.java使用未经检查或不安全的操作。”我正试图尽我所能摆脱它,但似乎没有任何效果。基类编译得很好,没有警告,检查似乎来自addall方法。有什么想法吗Java 使用泛型并尝试消除不安全操作警告,java,generics,compiler-warnings,Java,Generics,Compiler Warnings,这是我的代码,但我一直得到“注意:LinkedListAdd.java使用未经检查或不安全的操作。”我正试图尽我所能摆脱它,但似乎没有任何效果。基类编译得很好,没有警告,检查似乎来自addall方法。有什么想法吗 public class LinkedListAdd<E extends Comparable <E> > extends LinkedList<E> { public LinkedListAdd() { super(); }
public class LinkedListAdd<E extends Comparable <E> > extends LinkedList<E>
{
public LinkedListAdd()
{
super();
}
public <E extends Comparable<E> > boolean addAll(final int index, final E[] array)
{
if(index < 0 || index > this.size())
throw new IndexOutOfBoundsException("Index out of bounds");
if(array == null)
throw new NullPointerException("Null array");
Node nn = null;
for(int y = array.length - 1; y >= 0; y--)
{
nn = new Node(array[y], null);
if(this.size() == 0)
{
this.head.next = nn;
this.size += 1;
}
else
{
Node cur = this.head.next, prev = null;
for(int x = 0; x < index; x++)
{
prev = cur;
cur = cur.next;
}
if(prev == null)
{
nn.next = cur;
this.head.next = nn;
this.size += 1;
}
else
{
prev.next = nn;
nn.next = cur;
this.size += 1;
}
}
}
if(nn == null)
return false;
return true;
}
}//end class LinkedListSort
公共类LinkedList添加扩展LinkedList
{
公共LinkedListAdd()
{
超级();
}
公共布尔addAll(最终整数索引,最终E[]数组)
{
if(index<0 | | index>this.size())
抛出新的IndexOutOfBoundsException(“索引超出范围”);
if(数组==null)
抛出新的NullPointerException(“Null数组”);
节点nn=null;
对于(int y=array.length-1;y>=0;y--)
{
nn=新节点(数组[y],空);
if(this.size()==0)
{
this.head.next=nn;
此值为1.size+=1;
}
其他的
{
节点cur=this.head.next,prev=null;
对于(int x=0;x
这是基类
public class LinkedList<E extends Comparable <E> >
{
protected static class Node<E extends Comparable <E> >
{
public E data;
public Node<E> next;
public Node()
{
this.next = null;
this.data = null;
}// end DVC
public Node(final E data, final Node<E> next)
{
this.next = next;
this.data = data;
}// end EVC
}// end class Node
protected Node <E> head;
protected int size;
public LinkedList()
{
this.head = new Node<>();
this.size = 0;
}
public void clear()
{
this.head = null;
this.size = 0;
}// end clear
public int size(){return this.size;}
public void addFirst(final E data)
{
this.head.next = new Node<>(data, this.head.next);
size ++;
}// end
public String toString()
{
String temp = "List size: " + this.size;
if(this.head.next == null)
return temp += "\nEmpty List";
else
{
temp += "\n";
Node<E> cur = head.next;
while(cur != null)
{
temp += cur.data + " ";
cur = cur.next;
}
return temp;
}// end else
}// end toString
}// end class
公共类链接列表
{
受保护的静态类节点
{
公共电子数据;
公共节点下一步;
公共节点()
{
this.next=null;
this.data=null;
}//末端DVC
公共节点(最终E数据,最终节点下一个)
{
this.next=next;
这个数据=数据;
}//结束EVC
}//端类节点
受保护节点头;
保护整数大小;
公共链接列表()
{
this.head=新节点();
此值为0.size=0;
}
公共空间清除()
{
this.head=null;
此值为0.size=0;
}//了结
public int size(){返回this.size;}
公共void addFirst(最终E数据)
{
this.head.next=新节点(数据,this.head.next);
大小++;
}//结束
公共字符串toString()
{
String temp=“列表大小:”+this.size;
if(this.head.next==null)
返回临时值+=“\n临时列表”;
其他的
{
温度+=“\n”;
节点cur=head.next;
while(cur!=null)
{
温度+=当前数据+“”;
cur=cur.next;
}
返回温度;
}//结束其他
}//端到串
}//末级
我建议做几件事;首先,我要为get/set-on-your-head/next/size/data添加方法,并将这些字段设置为私有字段
关于你的仿制药
方法签名应该是
public boolean addAll(final int index, final E[] array)
并且您对该方法中的节点的声明需要
Node<E> node
节点
节点类是泛型的吗?你能包含节点的代码吗?是的,对不起,我刚刚添加了基类作为初学者,它看起来肯定需要引用节点
,而不仅仅是节点
…啊,你肯定还需要在addAll
方法的声明中去掉
。