아래 프로그램에서 DList1
은 목록 추상화입니다.다른 유형의 객체를 비교하는 방법은 무엇입니까?
DList1
클래스의 main() 함수에서 일부 단위 테스트 케이스를 실행하고 싶습니다. Incompatible operand types Object and int
:
Line 105
에서
main()
방법에서
, l.head.item != 9
, 구체적으로, 오류를 컴파일 할 수 있습니다.
컴파일시 값 9
은 기본 유형 int
이고 l.head.item
은 class Object
입니다.
런타임시 l.head.item
은 class Integer
입니다. 값 유형 9
에 대해 확실하지 않습니다.
/* DList1.java */
/**
* A DList1 is a mutable doubly-linked list. (No sentinel, not
* circularly linked.)
*/
public class DList1 {
/**
* head references the first node.
* tail references the last node.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
protected DListNode1 head;
protected DListNode1 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(Object item) {
if(this.head == null){
this.head = new DListNode1(item);
this.tail = this.head;
}else{
DListNode1 newNode = new DListNode1(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 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 l = new DList1();
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 105
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.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.");
}
}
}
/* DListNode1.java */
/**
* A DListNode1 is a node in a DList1 (doubly-linked list).
*/
class DListNode1 {
/**
* 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.
*/
Object item;
DListNode1 prev;
DListNode1 next;
/**
* DListNode1() constructor.
*/
DListNode1() {
this.item = null;
this.prev = null;
this.next = null;
}
DListNode1(Object item) {
this.item = item;
this.prev = null;
this.next = null;
}
}
내 질문 :
내가 모두 컴파일 타임에 유형이 호환 및 시간을 실행하고 값이Line 105
에서 비교하자 어떻게
? 값이 9
인 실행 시간 유형은 무엇입니까?
정수로 캐스팅하고, 당신의 갱신'head.item'에서 l.item.head.equals (9) – sscnapoli1926
sscnapoli1926 @ 작은 오타. 내 두 번째 질문은 어떨까요? – overexchange
"!" 오타가 아니라 오바이트 연산자입니다! 어쨌든 런타임에 항목의 유형을 결정해야하는 경우 객체 필드를 제네릭으로 바꿀 수있는 유일한 방법입니다. "item"이 클래스의 인스턴스가 될 수 있다면 equals() 재정의를 제공해야하고 게임이 완료됩니다 – sscnapoli1926