Java 在链接列表中存储对象
我被要求从头开始创建和管理链接列表,而不使用Javautil 如果我要创建一个具有多个属性(例如名称和年龄)的对象,那么是否可以将该对象存储在链接列表中 我很难理解这一点,希望能得到任何帮助 以下是我的伪代码: 课程: 节点 利斯特 人 地址 然后,我将如何在同一个链接列表中将这两个信息链接在一起Java 在链接列表中存储对象,java,linked-list,Java,Linked List,我被要求从头开始创建和管理链接列表,而不使用Javautil 如果我要创建一个具有多个属性(例如名称和年龄)的对象,那么是否可以将该对象存储在链接列表中 我很难理解这一点,希望能得到任何帮助 以下是我的伪代码: 课程: 节点 利斯特 人 地址 然后,我将如何在同一个链接列表中将这两个信息链接在一起 编辑:谢谢大家的意见,我会根据你们的建议好好阅读的!再次表示感谢!:) 您应该编写自己的使用泛型的LinkedList;让它处理任何对象类型 您的姓名、邮政编码、年龄等等不应该封装在一个可以添加到Li
编辑:谢谢大家的意见,我会根据你们的建议好好阅读的!再次表示感谢!:) 您应该编写自己的使用泛型的LinkedList;让它处理任何对象类型 您的姓名、邮政编码、年龄等等不应该封装在一个可以添加到LinkedList的对象中
package linkedlist;
public class Node<T> {
private Node<T> prev;
private Node<T> next;
private T value;
}
包链接列表;
公共类节点{
私有节点prev;
私有节点下一步;
私人T值;
}
您应该编写自己的使用泛型的LinkedList;让它处理任何对象类型
您的姓名、邮政编码、年龄等等不应该封装在一个可以添加到LinkedList的对象中
package linkedlist;
public class Node<T> {
private Node<T> prev;
private Node<T> next;
private T value;
}
包链接列表;
公共类节点{
私有节点prev;
私有节点下一步;
私人T值;
}
这并不难,您只需要创建自己的节点类。该类可能如下所示:
public class Node{
protected String name;
protected int age;
//any additional data you need...
protected Node next;
//methods...
public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}
public class MyLinkedList{
MyNode head; //this is a reference to the top element of your list
int nodeCount //
//put all the requkired methods here
}
此类将包含许多数据字段,并提供与这些字段交互的方法。关键组件是“protected Node next;”行,它是链接列表中的下一个节点。除了尾部,列表中的所有节点都将有下一个节点。tail节点将next设置为null。这并不难,您只需要创建自己的节点类。该类可能如下所示:
public class Node{
protected String name;
protected int age;
//any additional data you need...
protected Node next;
//methods...
public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}
public class MyLinkedList{
MyNode head; //this is a reference to the top element of your list
int nodeCount //
//put all the requkired methods here
}
此类将包含许多数据字段,并提供与这些字段交互的方法。关键组件是“protected Node next;”行,它是链接列表中的下一个节点。除了尾部,列表中的所有节点都将有下一个节点。tail节点将next设置为null。尝试查找什么是链表以及需要如何构造链表。您的psuedo代码与链表无关,只是一些基本的数据条目。我建议您查看以下链接以了解它是什么以及它是如何工作的。一旦理解了结构,实际的编码就相当简单了 我鼓励别人不要为你做家庭作业
尝试查找什么是链表以及需要如何构造链表。您的psuedo代码与链表无关,只是一些基本的数据条目。我建议您查看以下链接以了解它是什么以及它是如何工作的。一旦理解了结构,实际的编码就相当简单了 我鼓励别人不要为你做家庭作业
首先,您需要定义一个链接列表的基本建筑群,它是节点。节点就像容器,可以存储您想要的任何内容。这就是storedData变量为Object类型的原因。您可以这样定义它:
public class Node{
protected String name;
protected int age;
//any additional data you need...
protected Node next;
//methods...
public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}
public class MyLinkedList{
MyNode head; //this is a reference to the top element of your list
int nodeCount //
//put all the requkired methods here
}
然后可以定义链表类,如下所示:
public class Node{
protected String name;
protected int age;
//any additional data you need...
protected Node next;
//methods...
public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}
public class MyLinkedList{
MyNode head; //this is a reference to the top element of your list
int nodeCount //
//put all the requkired methods here
}
首先,您需要定义一个链接列表的基本构建块,它是节点。节点就像容器,可以存储您想要的任何内容。这就是storedData变量为Object类型的原因。您可以这样定义它:
public class Node{
protected String name;
protected int age;
//any additional data you need...
protected Node next;
//methods...
public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}
public class MyLinkedList{
MyNode head; //this is a reference to the top element of your list
int nodeCount //
//put all the requkired methods here
}
然后可以定义链表类,如下所示:
public class Node{
protected String name;
protected int age;
//any additional data you need...
protected Node next;
//methods...
public class MyNode{
Object storedData; // this is a reference to the object that you want stored in the list
MyNode next; //this is a reference to the next node in your list
...
}
public class MyLinkedList{
MyNode head; //this is a reference to the top element of your list
int nodeCount //
//put all the requkired methods here
}
谁会让你做这样可怕的事?是时候摆脱那种关系了!看这里:如果这是家庭作业,它应该被标记为这样。我假设它是家庭作业,并标记为这样。不太可能有其他原因。谁会让你做这样可怕的事?是时候摆脱那种关系了!看这里:如果这是家庭作业,它应该被标记为这样。我假设它是家庭作业,并标记为这样。可能没有其他原因。学习经验我猜:)学习链表比实现链表更好吗?学习经验我猜:)学习链表比实现链表更好吗?请不要将数据结构与数据混合。姓名、年龄等不是
节点
类的成员。请不要将数据结构与数据混合。姓名、年龄等不是节点
类的成员。