0

I've grown acostumed to use c++ pass by reference and now I'm starting to code more in Java. When solving a problem at leetcode I faced a problem: I tried to use this piece of code so I didn't have to rewrite this three lines often.

public void copyAndMoveNodes(ListNode tail, ListNode nodeToCopy) {
    tail.next = new ListNode(nodeToCopy.val);
    tail = tail.next;
    nodeToCopy = nodeToCopy.next;
}

I was expecting it to work similarly this in c++:

void copyAndMoveNodes(ListNode* &tail, ListNode* &nodeToCopy) {
    ListNode newNode = new ListNode(nodeToCopy.val);
    tail->next = &newNode;
    tail = tail->next;
    nodeToCopy = nodeToCopy->next;
}

But I discovered I passed just the copy of the objects' references in java as writen in the first piece of code.

Is it possible to rewrite this in Java to work similarly? I use this sequence of three commands many times and I don't want to write them every time, it makes the code more confusing.

  • 2
    Does this answer your question? [How to do the equivalent of pass by reference for primitives in Java](https://stackoverflow.com/questions/5614562/how-to-do-the-equivalent-of-pass-by-reference-for-primitives-in-java) – Scheff's Cat Nov 12 '19 at 11:12
  • [SO: How can I simulate pass by reference in Java?](https://stackoverflow.com/q/7884581/7478597) – Scheff's Cat Nov 12 '19 at 11:14
  • [SO: Java pass by reference](https://stackoverflow.com/q/9404625/7478597) – Scheff's Cat Nov 12 '19 at 11:15

1 Answers1

0

The solution is an additional return value - for a mechanical translation.

param = f(param, x);

Very ugly when having two references:

  • a void method with a parameter ListNode[]
  • a void method with a parameter Pair<ListNode, ListNode>

Best would be give a Stream a try. On collection classes no problem, on node classes holding a next field, it can still be done as:

Stream.iterate(rootToCopy, node -> node.next != null, node -> node.next)
    .forEach(node -> { ... });

Even if it would be on a Pair of nodes or such.

So my conclusion: switch to the java collection classes and operate on entire lists using .stream(). Knowing both sides C++ with the power of references, and java, I do not find it hard to have the more abstract style in java: performance still okay, readability fine, writability needing excercise, expressiveness and compactness excellent.

Porting the software in the first stage could be done best by inlining the method's code.

One apology for java: by language design f(x) will never change the variable x to improve software quality / code checkers.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • Are your comments concerning _performance, readability..._ about Java? Well, just type _'why is java'_ in the google search and see the suggestions ;) – nada Nov 12 '19 at 14:59
  • @nada I understand I was overdoing a bit. I am not biased towards Java. The main point is that the mechanical translation (as nicely suggested by Scheff) often introduces more verbose code (than C++), and that is insatisfactory. I especially wanted to rewriting with java collections a bit more alluring. Reformulated slightly – Joop Eggen Nov 12 '19 at 15:08