简体   繁体   中英

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.

So, I was planning to use LinkedList of ArrayList for this. ( LinkedList<ArrayList<String>> or sth...)

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.

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.

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?

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. 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. (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
  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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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