Java类与泛型数据类型

huangapple go评论105阅读模式
英文:

Java class with generic data types

问题

我正在尝试编写一个简单的链表类,该类使用了泛型数据类型。我是Java的新手,所以我不知道如何调试主方法中出现的错误消息,该错误消息在我尝试将数据插入我的类的实例时出现。类代码和主方法如下所示:

  1. import java.io.*;
  2. // Java program to implement
  3. // a Singly Linked List
  4. public class MyLinkedList<T> {
  5. Node head; // 链表的头部
  6. class Node<T> {
  7. T data;
  8. Node next;
  9. // 构造函数
  10. Node(T d)
  11. {
  12. data = d;
  13. next = null;
  14. }
  15. }
  16. // 插入新节点的方法
  17. MyLinkedList insert(MyLinkedList list, T data)
  18. {
  19. // 使用给定数据创建一个新节点
  20. Node new_node = new Node(data);
  21. new_node.next = null;
  22. // 如果链表为空,
  23. // 则将新节点设为头部
  24. if (list.head == null) {
  25. list.head = new_node;
  26. }
  27. else {
  28. // 否则遍历到最后一个节点
  29. // 并在那里插入新节点
  30. Node last = list.head;
  31. while (last.next != null) {
  32. last = last.next;
  33. }
  34. // 在最后一个节点处插入新节点
  35. last.next = new_node;
  36. }
  37. // 通过头部返回链表
  38. return list;
  39. }
  40. // 驱动程序代码
  41. public static void main(String[] args)
  42. {
  43. /* 从空链表开始。 */
  44. MyLinkedList list = new MyLinkedList();
  45. // 插入值
  46. list = insert(list, 1);
  47. }
  48. }

请注意,我已经修复了代码中的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:

  1. import java.io.*;
  2. // Java program to implement
  3. // a Singly Linked List
  4. public class MyLinkedList&lt;T&gt; {
  5. Node head; // head of the list
  6. class Node&lt;T&gt; {
  7. T data;
  8. Node next;
  9. // Constructor
  10. Node(T d)
  11. {
  12. data = d;
  13. next = null;
  14. }
  15. }
  16. // Method to insert a new node
  17. MyLinkedList insert(MyLinkedList list, T data)
  18. {
  19. // Create a new node with given data
  20. Node new_node = new Node(data);
  21. new_node.next = null;
  22. // If the Linked List is empty,
  23. // then make the new node as head
  24. if (list.head == null) {
  25. list.head = new_node;
  26. }
  27. else {
  28. // Else traverse till the last node
  29. // and insert the new_node there
  30. Node last = list.head;
  31. while (last.next != null) {
  32. last = last.next;
  33. }
  34. // Insert the new_node at last node
  35. last.next = new_node;
  36. }
  37. // Return the list by head
  38. return list;
  39. }
  40. // Driver code
  41. public static void main(String[] args)
  42. {
  43. /* Start with the empty list. */
  44. MyLinkedList list = new MyLinkedList();
  45. // Insert the values
  46. list = insert(list, 1);
  47. }
  48. }

答案1

得分: 3

  1. 你需要在类声明和方法签名中将 int 改为 T。
  1. class MyLinkedList<T> {
  2. MyLinkedList() {
  3. head = null;
  4. }
  5. Node<T> head; // head of list
  6. class Node<T> {
  7. T data;
  8. Node<T> next;
  9. // Constructor
  10. Node(T d) {
  11. data = d;
  12. next = null;
  13. }
  14. }
  15. // Method to insert a new node
  16. public void insert(T data) {
  17. // Create a new node with given data
  18. Node<T> new_node = new Node<T>(data);
  19. new_node.next = null;
  20. // If the Linked List is empty,
  21. // then make the new node as head
  22. if (this.head == null) {
  23. this.head = new_node;
  24. } else {
  25. Node<T> last = this.head;
  26. while (last.next != null) {
  27. last = last.next;
  28. }
  29. // Insert the new_node at last node
  30. last.next = new_node;
  31. }
  32. }
  33. protected void display() {
  34. Node<T> myNode = head;
  35. System.out.println();
  36. while (myNode != null) {
  37. System.out.println(myNode.data);
  38. myNode = myNode.next;
  39. }
  40. }
  41. }
  1. 将 insert 方法的签名更改如下:
  1. public static void main(String[] args) {
  2. /* Start with the empty list. */
  3. MyLinkedList<Integer> list = new MyLinkedList<Integer>();
  4. // Insert the values
  5. list.insert(1);
  6. list.insert(3);
  7. list.insert(12);
  8. list.insert(11);
  9. list.insert(21);
  10. list.insert(22);
  11. list.insert(45);
  12. list.display();
  13. }
  1. 为了编码清晰和理解,我已将类名更改为 MyLinkedList。
英文:
  1. You need to change int to T in class declaration and method signature.

    1. class MyLinkedList&lt;T&gt; {
    2. MyLinkedList() {
    3. head=null;
    4. }
    5. Node head; // head of list
    6. class Node&lt;T&gt; {
    7. T data;
    8. Node next;
    9. // Constructor
    10. Node(T d) {
    11. data = d;
    12. next = null;
    13. }

    }

    // Method to insert a new node

    1. public void insert(T data) {
    2. // Create a new node with given data
    3. Node new_node = new Node(data);
    4. new_node.next = null;
    5. // If the Linked List is empty,
    6. // then make the new node as head
    7. if (this.head == null) {
    8. this.head = new_node;
    9. } else {
    10. Node last = this.head;
    11. while (last.next != null) {
    12. last = last.next;
    13. }
    14. // Insert the new_node at last node
    15. last.next = new_node;
    16. }

    }

    1. protected void display() {
    2. Node myNode=head;
    3. System.out.println();
    4. while (myNode != null) {
    5. System.out.println(myNode.data);
    6. myNode=myNode.next;
    7. }

    }

  2. Change the insert method signature to the below:

    1. public static void main(String[] args) {
    2. /* Start with the empty list. */
    3. MyLinkedList&lt;Integer&gt; list = new MyLinkedList&lt;Integer&gt;();
    4. // Insert the values
    5. list.insert(1);
    6. list.insert(3);
    7. list.insert(12);
    8. list.insert(11);
    9. list.insert(21);
    10. list.insert(22);
    11. list.insert(45);
    12. list.display();
    13. }
  3. For clear coding and understanding I have changed class name as MyLinkedList

huangapple
  • 本文由 发表于 2020年7月30日 16:45:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/63169484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定