如何使用Java泛型避免投射? [英] How to use Java generics to avoid casting?
问题描述
对于在链接中引发的查询,Java泛型是建议避免在评估项目的运行时类型时遇到困难。
在以下代码中使用Java泛型后,我看不到与以前不兼容的类型错误。 p>
但是,在第96行
, DList1
不会给出任何线索。
错误消息是:令牌'int'的语法错误
/ * DList1.java * /
/ **
* DList1是一个可变的双向链表。 (No sentinel,不是
*循环链接。)
* /
公共类DList1< T> {
/ **
* head引用第一个节点。
* tail引用最后一个节点。
*
*请勿更改以下字段说明。
* /
受保护的DListNode1< T>头;
受保护的DListNode1< T>尾巴;
保护长尺寸;
/ * DList1不变式:
* 1)head.prev == null。
* 2)tail.next == null。
* 3)对于DList中的任何DListNode1 x,如果x.next == y和x.next!= null,则
*然后是y.prev == x。
* 4)对于DList中的任何DListNode1 x,如果x.prev == y和x.prev!= null,则
*然后是y.next == x。
* 5)可以通过一系列下一个
*引用从头部访问尾部。
* 6)大小是可以通过一系列下一个引用从
*头中访问的DListNode1的数量。
* /
/ **
*用于空DList1的DList1()构造函数。
* /
public DList1(){
this.head = null;
this.tail = null;
this.size = 0;
}
/ **
* insertFront()在DList1的前面插入一个项目。
* /
public void insertFront(T item){
if(this.head == null){
this.head = new DListNode1< T>(item);
this.tail = this.head;
} else {
DListNode1< T> newNode = new DListNode1< T>(item);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
this.size ++;
}
/ **
* removeFront()从DList1中删除第一个项目(和节点)。如果
*列表为空,则不执行任何操作。
* /
public void removeFront(){
if(this.size == 0){
return;
} else if(size == 1){
this.head = null;
this.tail = null;
} else {
this.head.next.prev = null;
this.head = this.head.next;
}
}
/ **
* toString()返回此DList的字符串表示形式。
*
*请勿更改此方法。
*
* @返回此DList的字符串表示形式。
* /
public String toString(){
String result =[;
DListNode1< T>当前=头;
while(current!= null){
result = result + current.item +;
current = current.next;
}
返回结果+];
}
public static void main(String [] args){
//不要更改以下代码。
DList1< int> l = new DList1< int>(); //第96行
System.out.println(### TESTING insertFront ### \ nEmpty list is+ l);
l.insertFront(9);
System.out.println(\\\
Inserting 9 at front.\\\
List with 9 is+ l);
if(l.head == null){
System.out.println(head is null。);
} else {
if(l.head.item!= 9){//第104行
System.out.println(head.item is wrong。);
}
if(l.head.prev!= null){
System.out.println(head.prev is wrong。);
if(l.tail == null){
System.out.println(tail is null。);
} else {
if(l.tail.item!= 9){
System.out.println(tail.item is wrong。);
}
if(l.tail.next!= null){
System.out.println(tail.next is wrong。);
if(l.size!= 1){
System.out.println(size is wrong。);
}
l.insertFront(8);
System.out.println(\\\
Inserting 8 at front.\\\
List with 8 and 9 is+ l);
if(l.head == null){
System.out.println(head is null。);
} else {
if(l.head.item!= 8){
System.out.println(head.item is wrong。);
}
if(l.head.prev!= null){
System.out.println(head.prev is wrong。);
if(l.head.next!= l.tail){
System.out.println(head.next is wrong。);
if(l.tail == null){
System.out.println(tail is null。);
} else {
/ * if(l.tail.item!= 9){
System.out.println(tail.item is wrong。);
} * /
if(l.tail.next!= null){
System.out.println(tail.next is wrong。);
}
if(l.tail.prev!= l.head){
System.out.println(tail.prev is wrong。);
if(l.size!= 2){
System.out.println(size is wrong。);
}
} / * end main()* /
}
/ * DListNode1.java * /
/ **
* A DListNode1是DList1(双向链表)中的一个节点。
* /
class DListNode1< T> {
/ **
*项目引用存储在当前节点中的项目。
* prev引用DList中的前一个节点。
* next引用DList中的下一个节点。
*
*请勿更改以下字段说明。
* /
T item;
DListNode1< T>上一张;
DListNode1< T>下一个;
/ **
* DListNode1()构造函数。
* /
DListNode1(){
this.item = null;
this.prev = null;
this.next = null;
}
DListNode1(T item){
this.item = item;
this.prev = null;
this.next = null;
code
我的问题:
这个错误在 96行
中的含义是什么?如何解决这个问题?
注意:使用jre 1.6更新45
解决方案在Java中,您不能使用像 int
这样的原始值对参数化类型进行参数化,而不是:
DList1<诠释> l = new DList1< int>();
使用包装类:
DList1<整数> l = new DList1< Integer>();
For the query, raised in link, Java generics is recommended to avoid difficulty in assessing run time type of an item.
After using Java generics in the below code, I do not see the incompatible type errors unlike before.
But a different compile time error at line 96
, DList1<int> l = new DList1<int>();
does not give any clue of the problem.
Error message is: Syntax error on token 'int'
/* DList1.java */
/**
* A DList1 is a mutable doubly-linked list. (No sentinel, not
* circularly linked.)
*/
public class DList1<T> {
/**
* head references the first node.
* tail references the last node.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
protected DListNode1<T> head;
protected DListNode1<T> tail;
protected long size;
/* DList1 invariants:
* 1) head.prev == null.
* 2) tail.next == null.
* 3) For any DListNode1 x in a DList, if x.next == y and x.next != null,
* then y.prev == x.
* 4) For any DListNode1 x in a DList, if x.prev == y and x.prev != null,
* then y.next == x.
* 5) The tail can be accessed from the head by a sequence of "next"
* references.
* 6) size is the number of DListNode1s that can be accessed from the
* head by a sequence of "next" references.
*/
/**
* DList1() constructor for an empty DList1.
*/
public DList1() {
this.head = null;
this.tail = null;
this.size = 0;
}
/**
* insertFront() inserts an item at the front of a DList1.
*/
public void insertFront(T item) {
if(this.head == null){
this.head = new DListNode1<T>(item);
this.tail = this.head;
}else{
DListNode1<T> newNode = new DListNode1<T>(item);
newNode.next = this.head;
this.head.prev = newNode;
this.head = newNode;
}
this.size++;
}
/**
* removeFront() removes the first item (and node) from a DList1. If the
* list is empty, do nothing.
*/
public void removeFront() {
if(this.size == 0){
return;
}else if(size ==1){
this.head = null;
this.tail = null;
}else{
this.head.next.prev = null;
this.head = this.head.next;
}
}
/**
* toString() returns a String representation of this DList.
*
* DO NOT CHANGE THIS METHOD.
*
* @return a String representation of this DList.
*/
public String toString() {
String result = "[ ";
DListNode1<T> current = head;
while (current != null) {
result = result + current.item + " ";
current = current.next;
}
return result + "]";
}
public static void main(String[] args) {
// DO NOT CHANGE THE FOLLOWING CODE.
DList1<int> l = new DList1<int>(); //Line 96
System.out.println("### TESTING insertFront ###\nEmpty list is " + l);
l.insertFront(9);
System.out.println("\nInserting 9 at front.\nList with 9 is " + l);
if (l.head == null) {
System.out.println("head is null.");
} else {
if (l.head.item != 9) { //Line 104
System.out.println("head.item is wrong.");
}
if (l.head.prev != null) {
System.out.println("head.prev is wrong.");
}
}
if (l.tail == null) {
System.out.println("tail is null.");
} else {
if (l.tail.item != 9) {
System.out.println("tail.item is wrong.");
}
if (l.tail.next != null) {
System.out.println("tail.next is wrong.");
}
}
if (l.size != 1) {
System.out.println("size is wrong.");
}
l.insertFront(8);
System.out.println("\nInserting 8 at front.\nList with 8 and 9 is " + l);
if (l.head == null) {
System.out.println("head is null.");
} else {
if (l.head.item != 8) {
System.out.println("head.item is wrong.");
}
if (l.head.prev != null) {
System.out.println("head.prev is wrong.");
}
if (l.head.next != l.tail) {
System.out.println("head.next is wrong.");
}
}
if (l.tail == null) {
System.out.println("tail is null.");
} else {
/*if (l.tail.item != 9) {
System.out.println("tail.item is wrong.");
}*/
if (l.tail.next != null) {
System.out.println("tail.next is wrong.");
}
if (l.tail.prev != l.head) {
System.out.println("tail.prev is wrong.");
}
}
if (l.size != 2) {
System.out.println("size is wrong.");
}
} /* end main() */
}
/* DListNode1.java */
/**
* A DListNode1 is a node in a DList1 (doubly-linked list).
*/
class DListNode1<T> {
/**
* item references the item stored in the current node.
* prev references the previous node in the DList.
* next references the next node in the DList.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
T item;
DListNode1<T> prev;
DListNode1<T> next;
/**
* DListNode1() constructor.
*/
DListNode1() {
this.item = null;
this.prev = null;
this.next = null;
}
DListNode1(T item) {
this.item = item;
this.prev = null;
this.next = null;
}
}
My question:
What is the meaning of this error at Line 96
? How do i resolve this?
Note: using jre 1.6 update 45
解决方案 In Java you can't parametrize a type with primitive values like int
, so instead of:
DList1<int> l = new DList1<int>();
use wrapper classes:
DList1<Integer> l = new DList1<Integer>();
这篇关于如何使用Java泛型避免投射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!