英文:
Java class with generic data types
问题
我正在尝试编写一个简单的链表类,该类使用了泛型数据类型。我是Java的新手,所以我不知道如何调试主方法中出现的错误消息,该错误消息在我尝试将数据插入我的类的实例时出现。类代码和主方法如下所示:
import java.io.*;
// Java program to implement
// a Singly Linked List
public class MyLinkedList<T> {
Node head; // 链表的头部
class Node<T> {
T data;
Node next;
// 构造函数
Node(T d)
{
data = d;
next = null;
}
}
// 插入新节点的方法
MyLinkedList insert(MyLinkedList list, T data)
{
// 使用给定数据创建一个新节点
Node new_node = new Node(data);
new_node.next = null;
// 如果链表为空,
// 则将新节点设为头部
if (list.head == null) {
list.head = new_node;
}
else {
// 否则遍历到最后一个节点
// 并在那里插入新节点
Node last = list.head;
while (last.next != null) {
last = last.next;
}
// 在最后一个节点处插入新节点
last.next = new_node;
}
// 通过头部返回链表
return list;
}
// 驱动程序代码
public static void main(String[] args)
{
/* 从空链表开始。 */
MyLinkedList list = new MyLinkedList();
// 插入值
list = insert(list, 1);
}
}
请注意,我已经修复了代码中的HTML编码错误,使其在Java中正常运行。
英文:
I am trying to write a simple linked list class that uses generic data type. I am new to Java so I can't figure out how to debug the error message in the main method that arises when I try to insert data into an instance of my class. The class code and the main method are as follows:
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);
}
}
答案1
得分: 3
- 你需要在类声明和方法签名中将 int 改为 T。
class MyLinkedList<T> {
MyLinkedList() {
head = null;
}
Node<T> head; // head of list
class Node<T> {
T data;
Node<T> next;
// Constructor
Node(T d) {
data = d;
next = null;
}
}
// Method to insert a new node
public void insert(T data) {
// Create a new node with given data
Node<T> new_node = new Node<T>(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<T> 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<T> myNode = head;
System.out.println();
while (myNode != null) {
System.out.println(myNode.data);
myNode = myNode.next;
}
}
}
- 将 insert 方法的签名更改如下:
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();
}
- 为了编码清晰和理解,我已将类名更改为 MyLinkedList。
英文:
-
You need to change int to T in class declaration and method signature.
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; }
}
// Method to insert a new node
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; }
}
-
Change the insert method signature to the below:
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(); }
-
For clear coding and understanding I have changed class name as MyLinkedList
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论