Java-get()方法中的单链表
我是Java新手,正在尝试用Java实现单链表。我已经包括了泛型的使用。代码如下所示:Java-get()方法中的单链表,java,generics,Java,Generics,我是Java新手,正在尝试用Java实现单链表。我已经包括了泛型的使用。代码如下所示: public class LinkedListX<E>{ Node <E> head; int size; LinkedListX(){ head = null; size = 0; } LinkedListX (E e){ head = new Node(e); size = 1; }
public class LinkedListX<E>{
Node <E> head;
int size;
LinkedListX(){
head = null;
size = 0;
}
LinkedListX (E e){
head = new Node(e);
size = 1;
}
void add(E e){
if (head == null){
head = new Node (e);
} else {
Node current = this.head;
while (current.next != null){
current = current.next;
}
current.next = new Node(e);
}
size++;
}
E get (int n){
if (n > size){
return null;
}
Node current = head;
for (int i=1;i<n;i++){
current = current.next;
}
return current.e;
}
private class Node<E> {
private E e;
private Node next;
Node (E e, Node n){
this.e = e;
this.next = n;
}
Node (E e) {
this.e = e;
}
}
公共类LinkedListX{
节点头;
整数大小;
LinkedListX(){
head=null;
尺寸=0;
}
LinkedListX(E){
头部=新节点(e);
尺寸=1;
}
无效添加(E){
if(head==null){
头部=新节点(e);
}否则{
节点电流=this.head;
while(current.next!=null){
当前=当前。下一步;
}
current.next=新节点(e);
}
大小++;
}
E get(int n){
如果(n>大小){
返回null;
}
节点电流=头;
对于(int i=1;i您将节点存储为next
,而不使用其通用属性-在许多地方您应该使用节点
,而不仅仅是节点
基本上,问题是您试图返回E
,但是您保存了节点
,该节点由编译器映射到节点
,对象
不是E
,要正确使用泛型,您可以更改:
- 在
get()
:Node current=head;
到Node current=head;
- 所有
新节点()
到新节点()
(新节点()的快捷方式)
- 在
节点
类中也使用
您也可以从所有的节点
中删除
(对于所有的节点类),您仍然可以使用E
获取数据)由于节点
是一个内部类,因此它还可以访问外部类的泛型参数。并且您永远不会为E
指定与外部类不同的值。因此只需从节点
类声明中删除
:
private class Node{
// the rest
}
你会遇到两个问题:
该行导致问题(类型不匹配上方的4行):
您还必须包括泛型,否则,它的原始类型将被识别为对象
Node<E> current = head;
否则,将出现警告投诉:
类型参数E正在隐藏类型E
您的代码中有许多原始的节点引用,这些引用正在生成许多警告。在您的get(int n)
snippet中Node
是原始的,不是泛型的,因此Java类型推断算法无法将其识别为E并将其视为对象。请使用下面的代码并在Eclipse中检查差异
public class LinkedListX<E> {
Node<E> head;
int size;
LinkedListX() {
head = null;
size = 0;
}
LinkedListX(E e) {
head = new Node<E>(e);
size = 1;
}
void add(E e) {
if (head == null) {
head = new Node<E>(e);
} else {
Node<E> current = this.head;
while (current.next != null) {
current = current.next;
}
current.next = new Node<E>(e);
}
size++;
}
E get(int n) {
if (n > size) {
return null;
}
Node<E> current = head;
for (int i = 1; i < n; i++) {
current = current.next;
}
return current.e;
}
static private class Node<E> {
private E e;
private Node<E> next;
@SuppressWarnings("unused")
Node(E e, Node<E> n) {
this.e = e;
this.next = n;
}
Node(E e) {
this.e = e;
}
}
}
公共类LinkedListX{
节点头;
整数大小;
LinkedListX(){
head=null;
尺寸=0;
}
LinkedListX(E){
头部=新节点(e);
尺寸=1;
}
无效添加(E){
if(head==null){
头部=新节点(e);
}否则{
节点电流=this.head;
while(current.next!=null){
当前=当前。下一步;
}
current.next=新节点(e);
}
大小++;
}
E get(int n){
如果(n>大小){
返回null;
}
节点电流=头;
对于(int i=1;i
上面的一行是原始类型声明,在这种情况下,“java.lang.Object”是默认类型变量
使用参数化类型NodeNode current=head;
将解决问题。Node current=head;
Node current=head;
通读泛型,特别是原始类型/类型擦除。这应该会澄清一些问题。另一方面,由于您的Node类是一个私有嵌套类,它只是为了要将值包装到一个公共对象中,您可以从中省略泛型,并且仍然使用封闭类类型参数E
private class Node {
// the implementation
}
public class LinkedListX<E> {
Node<E> head;
int size;
LinkedListX() {
head = null;
size = 0;
}
LinkedListX(E e) {
head = new Node<E>(e);
size = 1;
}
void add(E e) {
if (head == null) {
head = new Node<E>(e);
} else {
Node<E> current = this.head;
while (current.next != null) {
current = current.next;
}
current.next = new Node<E>(e);
}
size++;
}
E get(int n) {
if (n > size) {
return null;
}
Node<E> current = head;
for (int i = 1; i < n; i++) {
current = current.next;
}
return current.e;
}
static private class Node<E> {
private E e;
private Node<E> next;
@SuppressWarnings("unused")
Node(E e, Node<E> n) {
this.e = e;
this.next = n;
}
Node(E e) {
this.e = e;
}
}
}
Node current = head;