简体   繁体   English

如何访问Java LinkedList节点的私有字段?

[英]How can I access to the private fields of Java LinkedList Nodes?

I have to implement a board game, and it has 17 positions, and it is 'linked' like linked lists. 我必须实现一个棋盘游戏,它有17个位置,并且像链接列表一样被“链接”。

So, I was planning to use LinkedList of ArrayList for this. 因此,我打算为此使用ArrayList的LinkedList。 ( LinkedList<ArrayList<String>> or sth...) LinkedList<ArrayList<String>>或其他...)

However, it has two starting positions which merges after a few nodes, and players can opt which position they want to start from.(This means 'next' fields of two nodes point to one node, and a node might (but not necessary, I guess?) need two 'prev' fields.) 但是,它有两个起始位置,这些起始位置在几个节点之后合并,玩家可以选择要从哪个位置开始。(这意味着两个节点的“下一个”字段指向一个节点,并且一个节点可以(但不是必须的,我猜吗?)需要两个“上一个”字段。)

Also, I want to have the 'next' field of an additional node next to the last position point to itself. 另外,我想在最后一个位置点旁边指向另一个节点的“下一个”字段。

In short, I want a collection class that is similar to LinkedList, but with some mutation. 简而言之,我想要一个类似于LinkedList的集合类,但有一些变化。

How might I be able to do this? 我怎么能做到这一点?

My programming level is beginner~intermediate, and I have studied at LinkedList definitions similar to Java Collections Framework(but do not remember much of them...), and c++ std::list using textbooks. 我的编程水平是初学者到中级,并且我在LinkedList定义中进行了研究,类似于Java Collections Framework(但不记得很多),以及使用教科书的c ++ std :: list。

So my questions are these. 所以我的问题是这些。

Is it necessary for me to implement a simple List(and Node) class, and just create 18 Nodes and set these nodes' private fields in the way that I want to set, and add ArrayLists to its 'data' fields? 我是否有必要实现一个简单的List(和Node)类,仅创建18个Nodes并按照我想设置的方式设置这些节点的私有字段,并将ArrayLists添加到其“ data”字段中?

Also, I was to use LinkedList of Stack(because I only need the latest-pushed element), but I felt it could be more useful if I just create private ArrayList classes and tailor it to my flavor, in a way similar to implementing Stack using ArrayList classes. 另外,我将使用Stack的LinkedList(因为我只需要使用最新推送的元素),但是我觉得如果只创建私有ArrayList类并根据自己的喜好对其进行调整(类似于实现Stack的方式)可能会更有用。使用ArrayList类。 How does it sound? 听起来如何?

What I am worried about is that I am not a very good programmer, and if I define them by myself, they can be unreliable, and the job may be very time-consuming too.(it has deadline) 我担心的是我不是一个很好的程序员,如果我自己定义它们,它们可能会变得不可靠,并且工作可能也很耗时。

Is there any better way to achieve this?(like, maybe extending standard classes??not sure..) 有没有更好的方法来实现这一目标?(例如,可能扩展标准类?不确定。)

Any words of advice would be greatly appreciated. 任何建议的话将不胜感激。

Thank you. 谢谢。

I'm afraid your description of what your are trying to do (ie your requirements) is too hard to understand. 恐怕您对您要做什么的描述(即您的要求)很难理解。 So I'll restrict myself to a general answer. 因此,我将自己局限于一般性回答。

If you really need to be able to do "surgery" on your list-like data structure(s), then you should implement it / them yourself from scratch. 如果您确实确实需要能够对类似列表的数据结构进行“手术”,那么您应该自己/从头开始实施它。

Trying to do this by extending an existing list/collection class is likely to fail for a couple of reasons (at least). 尝试通过扩展现有列表/集合类来执行此操作可能会由于以下两个原因而失败(至少)。

  • Some of the stuff that your code needs to tinker with is declared as private , and that makes it very difficult (and highly inadvisable) to tinker with it. 您的代码需要修改的一些内容被声明为private ,这使得对其进行修改非常困难(并且非常不建议)。 (It is not impossible, but you'd need to do some nasty reflection tricks and that would make your code complicated, fragile and inefficient. Its is not the sort of thing you should do, unless there is absolutely no other alternative.) (这不是不可能的,但是您需要做一些讨厌的反射技巧,这会使您的代码复杂,脆弱和低效。除非绝对没有其他选择,否则这不是您应该做的事情。)

  • Even if you succeeded, you'd have a data structure that nominally implements one or more existing collection interfaces, but (most likely) breaks the contract of how that interface is supposed to behave. 即使您成功了,您也将拥有一个名义上实现一个或多个现有集合接口的数据结构,但是(很可能) 违反了该接口应该如何工作的约定


What I am worried about is that I am not a very good programmer, and if I define them by myself, they can be unreliable, and the job may be very time-consuming too. 我担心的是我不是一个很好的程序员,如果我自己定义它们,它们可能会变得不可靠,并且工作可能也很耗时。 (it has deadline) (有截止日期)

  1. The "point of the exercise" is to learn to be a better programmer ... “练习的重点”是学习成为一个更好的程序员。
  2. IMO, you are likely to spend more time doing the work to avoid the extra work, and the end result would be less reliable IMO,您可能会花费更多的时间进行工作以避免不必要的工作,并且最终结果将变得不太可靠
  3. Deadlines are a worry, but the best way to deal with them is to start early enough (assuming you can), and try to solve the problem the right way rather than taking risky short-cuts. 截止日期令人担忧,但是最好的解决方法是尽早开始(假设可以),并尝试以正确的方式解决问题,而不要冒险走捷径。

Anyway, my advice would be to try to do it the right way. 无论如何,我的建议是尝试以正确的方式进行操作。

  • If you fail to meet the deadline, you still have code that looks like a good attempt, and (one would hope) should be marked accordingly. 如果您未能按时完成任务,那么您仍然可以找到看起来不错的代码,并且应该(希望这样做)进行相应的标记。

  • On the other hand, if you try to do this the wrong way, you may get a working program but still be marked down on the basis that you've done it the wrong way. 另一方面,如果您尝试以错误的方式执行此操作,则可能会得到一个有效的程序,但仍会以错误的方式对其进行打标。 And you may fail, and be marked worse that if you had failed while trying to write the code properly. 而且,如果您在尝试正确编写代码时失败了,则可能会失败,并被标记为更糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM