Java class 具有通用数据类型
Java class with generic data types
我正在尝试编写一个使用通用数据类型的简单链表 class。我是 Java 的新手,所以我不知道如何调试在尝试将数据插入 class 的实例时出现的主要方法中的错误消息。 class代码及主要方法如下:
import java.io.*;
// Java program to implement
// a Singly Linked List
public class MyLinkedList<T> {
Node head; // head of the list
class Node<T> {
T data;
Node next;
// Constructor
Node(T d)
{
data = d;
next = null;
}
}
// Method to insert a new node
MyLinkedList insert(MyLinkedList list, T data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;
}
// Return the list by head
return list;
}
// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
MyLinkedList list = new MyLinkedList();
// Insert the values
list = insert(list, 1);
}
}
您需要将 class 声明和方法签名中的 int 更改为 T。
class MyLinkedList<T> {
MyLinkedList() {
head=null;
}
Node head; // head of list
class Node<T> {
T data;
Node next;
// Constructor
Node(T d) {
data = d;
next = null;
}
}
//插入新节点的方法
public void insert(T data) {
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
// If the Linked List is empty,
// then make the new node as head
if (this.head == null) {
this.head = new_node;
} else {
Node last = this.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;
}
}
protected void display() {
Node myNode=head;
System.out.println();
while (myNode != null) {
System.out.println(myNode.data);
myNode=myNode.next;
}
}
将插入方法签名更改为以下内容:
public static void main(String[] args) {
/* Start with the empty list. */
MyLinkedList<Integer> list = new MyLinkedList<Integer>();
// Insert the values
list.insert(1);
list.insert(3);
list.insert(12);
list.insert(11);
list.insert(21);
list.insert(22);
list.insert(45);
list.display();
}
为了清晰的编码和理解,我已将 class 名称更改为 MyLinkedList
我正在尝试编写一个使用通用数据类型的简单链表 class。我是 Java 的新手,所以我不知道如何调试在尝试将数据插入 class 的实例时出现的主要方法中的错误消息。 class代码及主要方法如下:
import java.io.*;
// Java program to implement
// a Singly Linked List
public class MyLinkedList<T> {
Node head; // head of the list
class Node<T> {
T data;
Node next;
// Constructor
Node(T d)
{
data = d;
next = null;
}
}
// Method to insert a new node
MyLinkedList insert(MyLinkedList list, T data)
{
// Create a new node with given data
Node new_node = new Node(data);
new_node.next = null;
// If the Linked List is empty,
// then make the new node as head
if (list.head == null) {
list.head = new_node;
}
else {
// Else traverse till the last node
// and insert the new_node there
Node last = list.head;
while (last.next != null) {
last = last.next;
}
// Insert the new_node at last node
last.next = new_node;
}
// Return the list by head
return list;
}
// Driver code
public static void main(String[] args)
{
/* Start with the empty list. */
MyLinkedList list = new MyLinkedList();
// Insert the values
list = insert(list, 1);
}
}
您需要将 class 声明和方法签名中的 int 更改为 T。
class MyLinkedList<T> { MyLinkedList() { head=null; } Node head; // head of list class Node<T> { T data; Node next; // Constructor Node(T d) { data = d; next = null; }
}
//插入新节点的方法
public void insert(T data) { // Create a new node with given data Node new_node = new Node(data); new_node.next = null; // If the Linked List is empty, // then make the new node as head if (this.head == null) { this.head = new_node; } else { Node last = this.head; while (last.next != null) { last = last.next; } // Insert the new_node at last node last.next = new_node; }
}
protected void display() { Node myNode=head; System.out.println(); while (myNode != null) { System.out.println(myNode.data); myNode=myNode.next; }
}
将插入方法签名更改为以下内容:
public static void main(String[] args) { /* Start with the empty list. */ MyLinkedList<Integer> list = new MyLinkedList<Integer>(); // Insert the values list.insert(1); list.insert(3); list.insert(12); list.insert(11); list.insert(21); list.insert(22); list.insert(45); list.display(); }
为了清晰的编码和理解,我已将 class 名称更改为 MyLinkedList