循环链表以及从节点内部访问上一个/下一个
我需要对象的循环列表。每个人都应该知道哪一个是上一个或下一个。我这样做了:
class Bus {
private Bus previous;
private Bus next;
public Bus() {
//anything
}
public void setPrevious(Bus bus) {
this.previous = bus;
}
public void setNext(Bus bus) {
this.next = bus;
}
private void someMethod() {
// if (previous.xxx() && next.xxx()) {
// do something
// }
}
}
我创建了一个 Bus 数组。将所有总线添加到其中后,我设置每个元素的下一个和上一个。我觉得它很丑:D。你能建议我更好的方法吗?
I need circular list of objects. And each one should know which is previous or next. I did this:
class Bus {
private Bus previous;
private Bus next;
public Bus() {
//anything
}
public void setPrevious(Bus bus) {
this.previous = bus;
}
public void setNext(Bus bus) {
this.next = bus;
}
private void someMethod() {
// if (previous.xxx() && next.xxx()) {
// do something
// }
}
}
And I created an array of Bus. After I add all buses into it, I set next and previous of each element. And I feel it's ugly:D. May you suggest me better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您调整
setNext
和setPrevious
方法,不仅更新其实例,还更新设置为next
和previous 的实例
你想要的必须依赖于外部机制。假设您最初创建了
Bus
A
和B
。当您调用A.setNext( B )
时,它还应该更新B
的前一个节点,而不必调用B.setPrevious( A )
。与在 Java 中向LinkedList
添加内容类似,您不必手动设置最后一个对象与刚刚添加的对象之间的链接。当然,您仍然必须考虑总线已经包含在另一个
List
中的情况,您也必须更新该List
。因此,按照其他响应之一的建议将节点与实际总线实例分开的建议是一个更好的主意。这允许您将总线添加到多个列表,并且可以更轻松地编写循环列表(或者仅使用列表的可用实现)。这也是更好的面向对象设计,因为您可以重用您编写的列表。
If you adjust your
setNext
andsetPrevious
methods to not only update their instance but also the instance that is set asnext
and asprevious
you want have to depend on an external mechanism.So let's say you have initially created
Bus
A
andB
. When you callA.setNext( B )
, it should also update the previous node ofB
, without having to callB.setPrevious( A )
. Similar as when you add something to aLinkedList
in Java, you do not have to manually set the link between the last object and the object you just added. Something likeOf course then you still have to consider the scenario where the bus is already contained in another
List
that you have to update thatList
as well.Therefore, the suggestion to separate the nodes from the actual bus instances as suggested in one of the other responses is a better idea. This allows you to add busses to multiple lists, and makes it probably easier to write your circular list (or just use an available implementation for the list). This is also better OO design as you can reuse the list you wrote.
只是一个想法,但也许有一个用于节点的类和一个用于列表的类。通过这种方式,您可以为节点类提供一个构造函数,用于处理设置其下一个和最后一个引用。这使得列表实际上只需要担心像
Add()
这样的标准列表函数。看一下列表界面。
Just a thought, but maybe have one class for the nodes and one for the list. In this way, you can have a constructor for the node class that handles setting its next and last references. This allows the list to really only need to worry about standard list functions like
Add()
.Take a look at the List interface.
链表不需要数组。要创建列表,您需要使用 next 和 previous 方法来连接对象并将最后一个与第一个连接(以进行循环)。和使用示例(使用您的实现):
或多或少是链表循环的想法。我建议查看另一个问题来理解链接列表。做圆形很容易。
A linked list don't need an array. To create the list you need to use next and previous methods to connect your objects and connect the last with the first (to do circular). And example of use (using your implementation):
That more or less the idea of a Linked List circular. I recommend to see that another question to understand a Linked List. To do it circular is easy.
为什么需要数组?您已经拥有每个对象中的引用。
Why do you need an array? You already have the references in each object.